Skip to content

Commit fd6b7b6

Browse files
committed
wip
1 parent 76389e3 commit fd6b7b6

32 files changed

+437
-289
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ vendor/
44
composer.lock
55
.phpunit.result.cache
66
.phpunit.cache
7+
test
8+

README.md

Lines changed: 91 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ We feel the package is currently feature complete, but feel free to send a pull
1919

2020
# Requirements
2121

22-
This package requires Laravel 6.0 or higher.
23-
24-
Please check the [Laravel support policy](https://laravel.com/docs/master/releases#support-policy) table for supported Laravel and PHP versions.
22+
This package requires Laravel 10 or 11.
2523

2624
# Installation
2725

@@ -34,7 +32,8 @@ composer require stackkit/laravel-database-emails
3432
Publish the configuration files.
3533

3634
```bash
37-
php artisan vendor:publish --tag=laravel-database-emails-config
35+
php artisan vendor:publish --tag=database-emails-config
36+
php artisan vendor:publish --tag=database-emails-migrations
3837
```
3938

4039
Create the database table required for this package.
@@ -65,68 +64,89 @@ protected function schedule(Schedule $schedule)
6564

6665
### Send an email
6766

67+
E-mails are composed the same way mailables are created.
68+
6869
```php
6970
<?php
7071

7172
use Stackkit\LaravelDatabaseEmails\Email;
73+
use Illuminate\Mail\Mailables\Content;
74+
use Stackkit\LaravelDatabaseEmails\Attachment;
75+
use Illuminate\Mail\Mailables\Envelope;
7276

7377
Email::compose()
74-
->label('welcome')
75-
->recipient('[email protected]')
76-
->subject('This is a test')
77-
->view('emails.welcome')
78-
->variables([
79-
'name' => 'John Doe',
78+
->content(fn (Content $content) => $content
79+
->view('tests::dummy')
80+
->with(['name' => 'John Doe'])
81+
)
82+
->envelope(fn (Envelope $envelope) => $envelope
83+
->subject('Hello')
84+
->from('[email protected]', 'John Doe')
85+
->to('[email protected]', 'Jane Doe')
86+
)
87+
->attachments([
88+
Attachment::fromStorageDisk('s3', '/invoices/john-doe/march-2024.pdf'),
8089
])
8190
->send();
91+
])
8292
```
8393

84-
### Specify multiple recipients
94+
### Sending emails to users in your application
8595

8696
```php
8797
<?php
88-
89-
use Stackkit\LaravelDatabaseEmails\Email;
90-
9198
Email::compose()
92-
->recipient([
93-
94-
95-
]);
99+
->user($user)
100+
->send();
96101
```
97102

98-
### CC and BCC
103+
By default, the `name` column will be used to set the recipient's name. If you wish to use another column, you should implement the `preferredEmailName` method in your model.
99104

100105
```php
101106
<?php
102107

103-
use Stackkit\LaravelDatabaseEmails\Email;
108+
use Illuminate\Database\Eloquent\Model;
104109

105-
Email::compose()
106-
107-
108-
109-
110+
class User extends Model
111+
{
112+
public function preferredEmailName(): string
113+
{
114+
return $this->first_name;
115+
}
116+
}
110117
```
111118

112-
### Reply-To
119+
By default, the `email` column will be used to set the recipient's e-mail address. If you wish to use another column, you should implement the `preferredEmailAddress` method in your model.
113120

114121
```php
115122
<?php
116123

117-
use Stackkit\LaravelDatabaseEmails\Email;
124+
use Illuminate\Database\Eloquent\Model;
118125

119-
Email::compose()
120-
126+
class User extends Model
127+
{
128+
public function preferredEmailAddress(): string
129+
{
130+
return $this->work_email;
131+
}
132+
}
133+
```
121134

122-
Email::compose()
123-
->replyTo(new Address('[email protected]', 'John Doe'));
135+
By default, the app locale will be used to set the recipient's locale. If you wish to use another column, you should implement the `preferredEmailLocale` method in your model.
124136

125-
Email::compose()
126-
->replyTo([
127-
new Address('[email protected]', 'John Doe'),
128-
new Address('[email protected]', 'Jane Doe'),
129-
]);
137+
```php
138+
<?php
139+
140+
use Illuminate\Database\Eloquent\Model;
141+
use Illuminate\Contracts\Translation\HasLocalePreference;
142+
143+
class User extends Model implements HasLocalePreference
144+
{
145+
public function preferredLocale(): string
146+
{
147+
return $this->locale;
148+
}
149+
}
130150
```
131151

132152
### Using mailables
@@ -145,36 +165,28 @@ Email::compose()
145165

146166
### Attachments
147167

148-
```php
149-
<?php
150-
151-
use Stackkit\LaravelDatabaseEmails\Email;
152-
153-
Email::compose()
154-
->attach('/path/to/file');
155-
```
168+
To start attaching files to your e-mails, you may use the `attach` method like you normally would in Laravel.
169+
However, you will have to use this package's `Attachment` class.
156170

157-
Or for in-memory attachments:
158171

159172
```php
160173
<?php
161174

162175
use Stackkit\LaravelDatabaseEmails\Email;
176+
use Stackkit\LaravelDatabaseEmails\Attachment;
163177

164178
Email::compose()
165-
->attachData('<p>Your order has shipped!</p>', 'order.html');
179+
->attachments([
180+
Attachment::fromPath(__DIR__.'/files/pdf-sample.pdf'),
181+
Attachment::fromPath(__DIR__.'/files/my-file.txt')->as('Test123 file'),
182+
Attachment::fromStorageDisk('my-custom-disk', 'test.txt'),
183+
])
184+
->send();
166185
```
167186

168-
### Custom Sender
169-
170-
```php
171-
<?php
172-
173-
use Stackkit\LaravelDatabaseEmails\Email;
174-
175-
Email::compose()
176-
->from('[email protected]', 'John Doe');
177-
```
187+
<small>
188+
Note: `fromData()` and `fromStorage()` are not supported as the work with raw data.
189+
</small>
178190

179191
### Scheduling
180192

@@ -189,27 +201,6 @@ Email::compose()
189201
->later('+2 hours');
190202
```
191203

192-
### Encryption (Optional)
193-
194-
If you wish to encrypt your e-mails, please enable the `encrypt` option in the configuration file. This is disabled by default. Encryption and decryption will be handled by Laravel's built-in encryption mechanism. Please note that by encrypting the e-mail it takes more disk space.
195-
196-
```text
197-
Without encryption
198-
199-
7 bytes (label)
200-
16 bytes (recipient)
201-
20 bytes (subject)
202-
48 bytes (view name)
203-
116 bytes (variables)
204-
1874 bytes (e-mail content)
205-
4 bytes (attempts, sending, failed, encrypted)
206-
57 bytes (created_at, updated_at, deleted_at)
207-
... x 10.000 rows = ± 21.55 MB
208-
209-
With encryption the table size is ± 50.58 MB.
210-
```
211-
212-
213204
### Queueing e-mails
214205

215206
**Important**: When queueing mail using the `queue` function, it is no longer necessary to schedule the `email:send` command. Please make sure it is removed from `app/Console/Kernel.php`.
@@ -235,22 +226,41 @@ Email::compose()
235226
->queue(null, null, now()->addMinutes(10));
236227
```
237228

238-
### Test mode (Optional)
229+
If you need more flexibility over how to queued mails are retried, please implement your own email job.
230+
231+
Within the job you can send the mail like this:
232+
233+
```php
234+
use Stackkit\LaravelDatabaseEmails\Sender;
235+
236+
(new Sender)->send($email);
237+
```
238+
239+
### Test mode
239240

240241
When enabled, all newly created e-mails will be sent to the specified test e-mail address. This is turned off by default.
241242

243+
```
244+
DATABASE_EMAILS_TESTING_ENABLED=true
245+
246+
```
247+
242248
### E-mails to send per minute
243249

244-
To configure how many e-mails should be sent each command, please check the `limit` option. The default is `20` e-mails every command.
250+
To configure how many e-mails should be sent each command.
251+
252+
```
253+
DATABASE_EMAILS_LIMIT=20
254+
```
245255

246-
### Send e-mails immediately (Optional)
256+
### Send e-mails immediately
247257

248258
Useful during development when Laravel Scheduler is not running
249259

250260
To enable, set the following environment variable:
251261

252262
```
253-
LARAVEL_DATABASE_EMAILS_SEND_IMMEDIATELY=true
263+
DATABASE_EMAILS_IMMEDIATELY=true
254264
```
255265

256266
### Pruning models

UPGRADING.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# From 6.x to 7.x
2+
3+
## Database changes (Impact: high)
4+
5+
The way addresses are stored in the database has changed. Therefore, emails created in 6.x and below are incompatible.
6+
7+
When you upgrade, the existing database table will be renamed to "emails_old" and a new table will be created.
8+
9+
## Creating emails (Impact: high)
10+
11+
The way emails are composed has changed and now borrows a lot from Laravel's mailable.
12+
13+
```php
14+
use Illuminate\Mail\Mailables\Content;
15+
use Stackkit\LaravelDatabaseEmails\Attachment;use Stackkit\LaravelDatabaseEmails\Email;
16+
use Illuminate\Mail\Mailables\Envelope;
17+
18+
Email::compose()
19+
->content(fn (Content $content) => $content
20+
->view('tests::dummy')
21+
->with(['name' => 'John Doe'])
22+
)
23+
->envelope(fn (Envelope $envelope) => $envelope
24+
->subject('Hello')
25+
->from('[email protected]', 'John Doe')
26+
->to('[email protected]', 'Jane Doe')
27+
)
28+
->attachments([
29+
Attachment::fromStorageDisk('s3', '/invoices/john-doe/march-2024.pdf'),
30+
])
31+
->send();
32+
])
33+
```
34+
35+
## Encryption (Impact: moderate)
36+
37+
E-mail encryption has been removed from the package.

config/laravel-database-emails.php renamed to config/database-emails.php

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@
1515

1616
'attempts' => 3,
1717

18-
/*
19-
|--------------------------------------------------------------------------
20-
| Encryption
21-
|--------------------------------------------------------------------------
22-
|
23-
| Here you may enable encryption for all e-mails. The e-mail will be encrypted according
24-
| your application's configuration (OpenSSL AES-256-CBC by default).
25-
|
26-
*/
27-
28-
'encrypt' => false,
29-
3018
/*
3119
|--------------------------------------------------------------------------
3220
| Test E-mail
@@ -40,9 +28,9 @@
4028

4129
'testing' => [
4230

43-
'email' => '[email protected]',
31+
'email' => env('DATABASE_EMAILS_TESTING_EMAIL'),
4432

45-
'enabled' => env('LARAVEL_DATABASE_EMAILS_TESTING_ENABLED', true),
33+
'enabled' => env('DATABASE_EMAILS_TESTING_ENABLED', false),
4634

4735
],
4836

@@ -57,7 +45,7 @@
5745
|
5846
*/
5947

60-
'limit' => 20,
48+
'limit' => env('DATABASE_EMAILS_LIMIT', 20),
6149

6250
/*
6351
|--------------------------------------------------------------------------
@@ -70,18 +58,5 @@
7058
|
7159
*/
7260

73-
'immediately' => env('LARAVEL_DATABASE_EMAILS_SEND_IMMEDIATELY', false),
74-
75-
/*
76-
|--------------------------------------------------------------------------
77-
| Manual migrations
78-
|--------------------------------------------------------------------------
79-
|
80-
| This option allows you to use:
81-
| `php artisan vendor:publish --tag=laravel-database-emails-migrations` to push migrations
82-
| to your app's folder, so you're free to modify before migrating.
83-
|
84-
*/
85-
86-
'manual_migrations' => (bool) env('LARAVEL_DATABASE_EMAILS_MANUAL_MIGRATIONS', false),
61+
'immediately' => env('DATABASE_EMAILS_IMMEDIATELY', false),
8762
];

0 commit comments

Comments
 (0)