sfneal / post-office
Laravel 应用程序的邮箱套件,具有扩展的 Mailable 和 Notification 功能
2.2.0
2024-04-23 18:22 UTC
Requires
- php: ^8.0|^8.1|^8.2|^8.3
- laravel/framework: ^8.75|^9.0|^10.0
- sfneal/queueables: ^2.0
Requires (Dev)
- orchestra/testbench: ^7.40|^8.0|9.0
- phpunit/phpunit: ^9.6|^10.0|^11.0
- scrutinizer/ocular: ^1.8
- sfneal/mock-models: ^0.5.0 || ^0.11.0
- sfneal/users: ^1.0|^2.0
README
Laravel 应用程序的邮箱套件,具有扩展的 Mailable 和 Notification 功能
安装
您可以通过 composer 安装此包。
composer require sfneal/post-office
要修改 post-office 配置文件和视图,请使用以下命令发布 ServiceProvider。
php artisan vendor:publish --provider="Sfneal\PostOffice\Providers\PostOfficeServiceProvider"
用法
创建 Mailable
并使用 SendMail
作业发送。
首先创建一个 Mailable
扩展并实现相应的 mailable 接口。
use Sfneal\PostOffice\Mailables\Interfaces\CallToAction; use Sfneal\PostOffice\Mailables\Interfaces\Email; use Sfneal\PostOffice\Mailables\Interfaces\Greeting; use Sfneal\PostOffice\Mailables\Interfaces\Message; use Sfneal\PostOffice\Mailables\Interfaces\Title; use Sfneal\PostOffice\Mailables\Mailable; use Sfneal\PostOffice\Mailables\Traits\UserMailable; use Sfneal\Users\Models\User; class InvoiceUnpaidMailable extends Mailable implements Greeting, Email, Title, Message, CallToAction { /** * @var User */ private $user; /** * @var int */ private $invoice_id; /** * InvoicePaidMailable constructor. * * @param User $user * @param int $invoice_id */ public function __construct(User $user, int $invoice_id) { $this->user = $user; $this->invoice_id = $invoice_id; parent::__construct( $this->getGreeting(), $this->getEmail(), $this->getTitle(), $this->getMessages(), $this->getCallToAction() ); } /** * @return string First line of email */ public function getGreeting(): string { return "Hi {$this->user->first_name}"; } /** * Email recipient. * * @return string */ public function getEmail(): string { return $this->user->email; } /** * Retrieve the Mailable's subject/title. * * @return string */ public function getTitle(): string { return "Unpaid Invoice: #{$this->invoice_id}"; } /** * Retrieve an array of messages to include in a mailable. * * @return array */ public function getMessages(): array { return [ 'You have one or more unpaid invoices. Please send use money asap!', "If your invoice is not paid within 30 days we're going to send a team of ninja's to your last known location.", ]; } /** * Call to Action button in the body of the email. * * @return array */ public function getCallToAction(): array { return [ 'url' => 'https://google.com', 'text' => 'Pay Invoice', ]; } }
然后,实例化您的 Mailable
并将其作为 $mailable 参数传递给 SendMail
作业。 SendMail
可以派发到作业队列或同步执行。
$mailable = new InvoiceUnpaidMailable($user, $invoice_id); // Dispatch SendMail job to the queue SendMail::dispatch($user->email, $mailable); // Execute the SendMail synchronously SendMail::dispatchSync($user->email, $mailable); // Execute the SendMail synchronously without using Queueable static methods $sent = (new SendMail($user->email, $mailable))->handle();
创建通知并使用 Notification
门面发送
首先创建一个定义您的通知的 Notification
扩展。
use Sfneal\PostOffice\Notifications\Notification; use Sfneal\Users\Models\User; class InvoiceUnpaidNotification extends Notification { /** * @var User */ public $user; /** * @var int */ public $invoice_id; /** * InvoicePaidMailable constructor. * * @param User $user * @param int $invoice_id */ public function __construct(User $user, int $invoice_id) { $this->user = $user; $this->invoice_id = $invoice_id; parent::__construct(); } /** * Get the mail representation of the notification. * * @param mixed $notifiable * @return InvoiceUnpaidMailable */ public function toMail($notifiable): InvoiceUnpaidMailable { return (new InvoiceUnpaidMailable($this->user, $this->invoice_id))->to($notifiable->email); } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable): array { return [ 'user_id' => $this->user->getKey(), 'invoice_id' => $this->invoice_id, ]; } }
使用 send()
或 sendNow()
方法发送通知。
$notification = new InvoiceUnpaidNotification($user, $invoice_id); // Send using the Job queue $notification->send($user); // Send synchronously $notification->sendNow($user);
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全
如果您发现任何安全问题,请通过电子邮件 stephen.neal14@gmail.com 而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。
PHP 包模板
此包是使用 PHP 包模板 生成的。