stackkit / laravel-database-emails
使用数据库存储和发送电子邮件
Requires
- ext-json: *
- doctrine/dbal: ^3.8
- laravel/framework: ^10.0|^11.0
Requires (Dev)
- laravel/pint: ^1.14
- mockery/mockery: ^1.2
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0
README
介绍
此包允许您使用数据库存储和发送电子邮件。
需求
此包需要Laravel 10或11。
安装
使用composer要求此包。
composer require stackkit/laravel-database-emails
发布配置文件。
php artisan vendor:publish --tag=database-emails-config php artisan vendor:publish --tag=database-emails-migrations
创建此包所需的数据库表。
php artisan migrate
将电子邮件定时任务添加到您的调度器中
protected function schedule(Schedule $schedule) { $schedule->command('email:send')->everyMinute()->withoutOverlapping(5); }
使用
发送电子邮件
电子邮件的创建方式与mailables相同。
use Stackkit\LaravelDatabaseEmails\Email; use Illuminate\Mail\Mailables\Content; use Stackkit\LaravelDatabaseEmails\Attachment; use Illuminate\Mail\Mailables\Envelope; Email::compose() ->content(fn (Content $content) => $content ->view('tests::dummy') ->with(['name' => 'John Doe']) ) ->envelope(fn (Envelope $envelope) => $envelope ->subject('Hello') ->from('johndoe@example.com', 'John Doe') ->to('janedoe@example.com', 'Jane Doe') ) ->attachments([ Attachment::fromStorageDisk('s3', '/invoices/john-doe/march-2024.pdf'), ]) ->send(); ])
向应用程序中的用户发送电子邮件
Email::compose() ->user($user) ->send();
默认情况下,将使用name
列来设置收件人的姓名。如果您想使用其他内容,应在模型中实现preferredEmailName
方法。
class User extends Model { public function preferredEmailName(): string { return $this->first_name; } }
默认情况下,将使用email
列来设置收件人的电子邮件地址。如果您想使用其他内容,应在模型中实现preferredEmailAddress
方法。
class User extends Model { public function preferredEmailAddress(): string { return $this->work_email; } }
默认情况下,将使用应用程序区域设置。如果您想使用其他内容,应在模型中实现preferredEmailLocale
方法。
class User extends Model implements HasLocalePreference { public function preferredLocale(): string { return $this->locale; } }
使用mailables
您还可以将mailable传递给电子邮件创作器。
Email::compose() ->mailable(new OrderShipped()) ->send();
附件
要开始将文件附加到电子邮件,您可以像通常在Laravel中那样使用attachments
方法。但是,您必须使用此包的Attachment
类。
use Stackkit\LaravelDatabaseEmails\Attachment; Email::compose() ->attachments([ Attachment::fromPath(__DIR__.'/files/pdf-sample.pdf'), Attachment::fromPath(__DIR__.'/files/my-file.txt')->as('Test123 file'), Attachment::fromStorageDisk('my-custom-disk', 'test.txt'), ]) ->send();
注意
Attachment::fromData()
和Attachment::fromStorage()
不受支持,因为它们与原始数据一起工作。
将模型附加到电子邮件
您可以将模型附加到电子邮件。这可以用来附加用户或其他属于电子邮件的模型。
Email::compose() ->model(User::find(1));
调度
您可以通过调用later
而不是send
来安排电子邮件。您必须提供Carbon实例或strtotime有效日期。
Email::compose() ->later('+2 hours');
队列电子邮件
重要
当使用queue
函数排队邮件时,不再需要安排email:send
命令。
Email::compose()->queue(); // On a specific connection Email::compose()->queue(connection: 'sqs'); // On a specific queue Email::compose()->queue(queue: 'email-queue'); // Delay (send mail in 10 minutes) Email::compose()->queue(delay: now()->addMinutes(10));
如果您需要更多灵活性,也可以传递您自己的作业类
Email::compose()->queue(jobClass: CustomSendEmailJob::class);
它可以看起来像这样
<?php namespace App\Jobs; use Illuminate\Contracts\Queue\ShouldQueue; use Stackkit\LaravelDatabaseEmails\SendEmailJob; class CustomSendEmailJob extends SendEmailJob implements ShouldQueue { // Define custom retries, backoff, etc... }
测试模式
当启用时,所有新创建的电子邮件都将发送到指定的测试电子邮件地址。默认情况下,此功能是关闭的。
DB_EMAILS_TESTING_ENABLED=true DB_EMAILS_TESTING_EMAIL=your-test-recipient@example.com
每分钟发送的电子邮件数
配置每次命令应发送多少封电子邮件。
DB_EMAILS_LIMIT=20
立即发送电子邮件
在Laravel调度器未运行时很有用
要启用,设置以下环境变量
DB_EMAILS_IMMEDIATELY=true
修剪模型
use Stackkit\LaravelDatabaseEmails\Email; $schedule->command('model:prune', [ '--model' => [Email::class], ])->daily();
默认情况下,电子邮件在超过6个月时将被修剪。
您可以通过在AppServiceProvider.php中添加以下内容来更改这一点
use Stackkit\LaravelDatabaseEmails\Email; public function register(): void { Email::pruneWhen(function (Email $email) { return $email->where(...); }); }