Skip to content

Commit 8e87ad0

Browse files
committed
wip
1 parent d41f0bb commit 8e87ad0

19 files changed

+207
-412
lines changed

README.md

Lines changed: 43 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Introduction
88

9-
This package allows you to store and send e-mails using a database.
9+
This package allows you to store and send e-mails using the database.
1010

1111
# Requirements
1212

@@ -16,34 +16,26 @@ This package requires Laravel 10 or 11.
1616

1717
Require the package using composer.
1818

19-
```bash
19+
```shell
2020
composer require stackkit/laravel-database-emails
2121
```
2222

2323
Publish the configuration files.
2424

25-
```bash
25+
```shell
2626
php artisan vendor:publish --tag=database-emails-config
2727
php artisan vendor:publish --tag=database-emails-migrations
2828
```
2929

3030
Create the database table required for this package.
3131

32-
```bash
32+
```shell
3333
php artisan migrate
3434
```
3535

3636
Add the e-mail cronjob to your scheduler
3737

3838
```php
39-
<?php
40-
41-
/**
42-
* Define the application's command schedule.
43-
*
44-
* @param \Illuminate\Console\Scheduling\Schedule $schedule
45-
* @return void
46-
*/
4739
protected function schedule(Schedule $schedule)
4840
{
4941
$schedule->command('email:send')->everyMinute()->withoutOverlapping(5);
@@ -58,8 +50,6 @@ protected function schedule(Schedule $schedule)
5850
E-mails are composed the same way mailables are created.
5951

6052
```php
61-
<?php
62-
6353
use Stackkit\LaravelDatabaseEmails\Email;
6454
use Illuminate\Mail\Mailables\Content;
6555
use Stackkit\LaravelDatabaseEmails\Attachment;
@@ -85,19 +75,14 @@ Email::compose()
8575
### Sending emails to users in your application
8676

8777
```php
88-
<?php
8978
Email::compose()
9079
->user($user)
9180
->send();
9281
```
9382

94-
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.
83+
By default, the `name` column will be used to set the recipient's name. If you wish to use something different, you should implement the `preferredEmailName` method in your model.
9584

9685
```php
97-
<?php
98-
99-
use Illuminate\Database\Eloquent\Model;
100-
10186
class User extends Model
10287
{
10388
public function preferredEmailName(): string
@@ -107,13 +92,9 @@ class User extends Model
10792
}
10893
```
10994

110-
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.
95+
By default, the `email` column will be used to set the recipient's e-mail address. If you wish to use something different, you should implement the `preferredEmailAddress` method in your model.
11196

11297
```php
113-
<?php
114-
115-
use Illuminate\Database\Eloquent\Model;
116-
11798
class User extends Model
11899
{
119100
public function preferredEmailAddress(): string
@@ -123,14 +104,9 @@ class User extends Model
123104
}
124105
```
125106

126-
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.
107+
By default, the app locale will be used. If you wish to use something different, you should implement the `preferredEmailLocale` method in your model.
127108

128109
```php
129-
<?php
130-
131-
use Illuminate\Database\Eloquent\Model;
132-
use Illuminate\Contracts\Translation\HasLocalePreference;
133-
134110
class User extends Model implements HasLocalePreference
135111
{
136112
public function preferredLocale(): string
@@ -145,27 +121,18 @@ class User extends Model implements HasLocalePreference
145121
You may also pass a mailable to the e-mail composer.
146122

147123
```php
148-
<?php
149-
150-
use Stackkit\LaravelDatabaseEmails\Email;
151-
152124
Email::compose()
153125
->mailable(new OrderShipped())
154126
->send();
155127
```
156128

157129
### Attachments
158130

159-
To start attaching files to your e-mails, you may use the `attach` method like you normally would in Laravel.
131+
To start attaching files to your e-mails, you may use the `attachments` method like you normally would in Laravel.
160132
However, you will have to use this package's `Attachment` class.
161133

162134

163135
```php
164-
<?php
165-
166-
use Stackkit\LaravelDatabaseEmails\Email;
167-
use Stackkit\LaravelDatabaseEmails\Attachment;
168-
169136
Email::compose()
170137
->attachments([
171138
Attachment::fromPath(__DIR__.'/files/pdf-sample.pdf'),
@@ -175,84 +142,82 @@ Email::compose()
175142
->send();
176143
```
177144

178-
<small>
179-
Note: `fromData()` and `fromStorage()` are not supported as the work with raw data.
180-
</small>
145+
> [!NOTE]
146+
> `Attachment::fromData()` and `Attachment::fromStorage()` are not supported as they work with raw data.
181147
182148
### Attaching models to e-mails
183149

184-
You may attach models to your e-mails.
150+
You may attach a model to an e-mail. This can be useful to attach a user or another model that belongs to the e-mail.
185151

186152
```php
187-
188153
Email::compose()
189154
->model(User::find(1));
190-
191155
```
192156

193157
### Scheduling
194158

195159
You may schedule an e-mail by calling `later` instead of `send`. You must provide a Carbon instance or a strtotime valid date.
196160

197161
```php
198-
<?php
199-
200-
use Stackkit\LaravelDatabaseEmails\Email;
201-
202162
Email::compose()
203163
->later('+2 hours');
204164
```
205165

206166
### Queueing e-mails
207167

208-
**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`.
168+
> [!IMPORTANT]
169+
> When queueing mail using the `queue` function, it is no longer necessary to schedule the `email:send` command.
209170
210171
```php
211-
<?php
172+
Email::compose()->queue();
212173

213-
use Stackkit\LaravelDatabaseEmails\Email;
174+
// On a specific connection
175+
Email::compose()->queue(connection: 'sqs');
214176

215-
Email::compose()
216-
->queue();
177+
// On a specific queue
178+
Email::compose()->queue(queue: 'email-queue');
217179

218-
// on a specific connection
219-
Email::compose()
220-
->queue('sqs');
180+
// Delay (send mail in 10 minutes)
181+
Email::compose()->queue(delay: now()->addMinutes(10));
182+
```
221183

222-
// on a specific queue
223-
Email::compose()
224-
->queue(null, 'email-queue');
184+
If you need more flexibility, you may also pass your own job class:
225185

226-
// timeout (send mail in 10 minutes)
227-
Email::compose()
228-
->queue(null, null, now()->addMinutes(10));
186+
```php
187+
Email::compose()->queue(jobClass: CustomSendEmailJob::class);
229188
```
230189

231-
If you need more flexibility over how to queued mails are retried, please implement your own email job.
232-
233-
Within the job you can send the mail like this:
190+
It could look like this:
234191

235192
```php
236-
use Stackkit\LaravelDatabaseEmails\Sender;
193+
<?php
194+
195+
namespace App\Jobs;
196+
197+
use Illuminate\Contracts\Queue\ShouldQueue;
198+
use Stackkit\LaravelDatabaseEmails\SendEmailJob;
237199

238-
(new Sender)->send($email);
200+
class CustomSendEmailJob extends SendEmailJob implements ShouldQueue
201+
{
202+
// Define custom retries, backoff, etc...
203+
}
239204
```
240205

241206
### Test mode
242207

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

245-
```
246-
DATABASE_EMAILS_TESTING_ENABLED=true
247-
DATABASE_EMAILS_TESTING_EMAIL[email protected]
210+
```dotenv
211+
DB_EMAILS_TESTING_ENABLED=true
212+
DB_EMAILS_TESTING_EMAIL[email protected]
248213
```
249214

250215
### E-mails to send per minute
251216

252217
To configure how many e-mails should be sent each command.
253218

254-
```
255-
DATABASE_EMAILS_LIMIT=20
219+
```dotenv
220+
DB_EMAILS_LIMIT=20
256221
```
257222

258223
### Send e-mails immediately
@@ -261,8 +226,8 @@ Useful during development when Laravel Scheduler is not running
261226

262227
To enable, set the following environment variable:
263228

264-
```
265-
DATABASE_EMAILS_IMMEDIATELY=true
229+
```dotenv
230+
DB_EMAILS_IMMEDIATELY=true
266231
```
267232

268233
### Pruning models
@@ -272,7 +237,7 @@ use Stackkit\LaravelDatabaseEmails\Email;
272237

273238
$schedule->command('model:prune', [
274239
'--model' => [Email::class],
275-
])->everyMinute();
240+
])->daily();
276241
```
277242

278243
By default, e-mails are pruned when they are older than 6 months.

UPGRADING.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,61 @@
11
# From 6.x to 7.x
22

3-
## Database changes (Impact: high)
3+
7.x is a bigger change which cleans up parts of the code base and modernizes the package. That means there are a few high-impact changes.
4+
5+
## Database changes (Impact: High)
46

57
The way addresses are stored in the database has changed. Therefore, emails created in 6.x and below are incompatible.
68

79
When you upgrade, the existing database table will be renamed to "emails_old" and a new table will be created.
810

9-
## Creating emails (Impact: high)
11+
The table migration now needs to be published first. Please run this command:
12+
13+
```shell
14+
php artisan vendor:publish --tag=database-emails-migrations
15+
```
16+
17+
Then, run the migration:
18+
19+
```shell
20+
php artisan migrate
21+
```
22+
23+
## Environment variables, configurations (Impact: High)
24+
25+
Environment variable names, as well as the config file name, have been shortened.
26+
27+
Please publish the new configuration file:
28+
29+
```shell
30+
php artisan vendor:publish --tag=database-emails-config
31+
```
32+
33+
You can remove the old configuration file.
34+
35+
Rename the following environments:
36+
37+
`LARAVEL_DATABASE_EMAILS_TESTING_ENABLED` `` `DB_EMAILS_TESTING_ENABLED`
38+
`LARAVEL_DATABASE_EMAILS_SEND_IMMEDIATELY` `->` `DB_EMAILS_SEND_IMMEDIATELY`
39+
40+
The following environments are new:
41+
42+
- `DB_EMAILS_ATTEMPTS`
43+
- `DB_EMAILS_TESTING_EMAIL`
44+
- `DB_EMAILS_LIMIT`
45+
- `DB_EMAILS_IMMEDIATELY`
46+
47+
The following environments have been removed:
48+
49+
- `LARAVEL_DATABASE_EMAILS_MANUAL_MIGRATIONS` because migrations are always published.
50+
51+
## Creating emails (Impact: High)
1052

1153
The way emails are composed has changed and now borrows a lot from Laravel's mailable.
1254

1355
```php
1456
use Illuminate\Mail\Mailables\Content;
15-
use Stackkit\LaravelDatabaseEmails\Attachment;use Stackkit\LaravelDatabaseEmails\Email;
57+
use Stackkit\LaravelDatabaseEmails\Attachment;
58+
use Stackkit\LaravelDatabaseEmails\Email;
1659
use Illuminate\Mail\Mailables\Envelope;
1760

1861
Email::compose()
@@ -32,6 +75,6 @@ Email::compose()
3275
])
3376
```
3477

35-
## Encryption (Impact: moderate)
78+
## Encryption (Impact: moderate/low)
3679

3780
E-mail encryption has been removed from the package.

config/database-emails.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
|
1414
*/
1515

16-
'attempts' => 3,
16+
'attempts' => env('DB_EMAILS_ATTEMPTS', 3),
1717

1818
/*
1919
|--------------------------------------------------------------------------
@@ -28,9 +28,9 @@
2828

2929
'testing' => [
3030

31-
'email' => env('DATABASE_EMAILS_TESTING_EMAIL'),
31+
'email' => env('DB_EMAILS_TESTING_EMAIL'),
3232

33-
'enabled' => env('DATABASE_EMAILS_TESTING_ENABLED', false),
33+
'enabled' => env('DB_EMAILS_TESTING_ENABLED', false),
3434

3535
],
3636

@@ -45,7 +45,7 @@
4545
|
4646
*/
4747

48-
'limit' => env('DATABASE_EMAILS_LIMIT', 20),
48+
'limit' => env('DB_EMAILS_LIMIT', 20),
4949

5050
/*
5151
|--------------------------------------------------------------------------
@@ -58,5 +58,5 @@
5858
|
5959
*/
6060

61-
'immediately' => env('DATABASE_EMAILS_IMMEDIATELY', false),
61+
'immediately' => env('DB_EMAILS_IMMEDIATELY', false),
6262
];

0 commit comments

Comments
 (0)