swiftmade / laravel-sendgrid-notification-channel
Sengrid.com的Laravel通知通道
Requires
- php: >=7.2
- illuminate/notifications: ^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0|^11.0
- sendgrid/sendgrid: ^7.11|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- mockery/mockery: ^1.5
- orchestra/testbench: ^5.0|^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^8.4|^9.0|^10.0
README
允许您使用Sendgrid的动态事务模板功能发送Laravel通知。
- 所需最低Laravel版本:7.x
- 所需最低PHP版本:7.2
(对于Laravel的较旧版本,请安装v1)
内容
安装
要开始,您需要需要此包
composer require swiftmade/laravel-sendgrid-notification-channel
Laravel将自动检测服务提供者。如果您已关闭自动发现,请将以下服务提供者在您的config/app.php
中添加。
NotificationChannels\SendGrid\SendGridServiceProvider::class,
接下来,请确保您在config/services.php
中有一个有效的Sendgrid API密钥。您可以将下面的示例配置复制过来以开始
return [ // other services... // add this... 'sendgrid' => [ 'api_key' => env('SENDGRID_API_KEY'), ], ];
使用
要使用动态模板发送电子邮件,您需要
- 在
via()
方法中返回SendGridChannel::class
。(不是mail
) - 添加并实现
toSendGrid($notifiable){ }
方法。
示例
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; use NotificationChannels\SendGrid\SendGridChannel; class ExampleNotification extends Notification { public function via($notifiable) { return [ SendGridChannel::class, // And any other channels you want can go here... ]; } // ... public function toSendGrid($notifiable) { return (new SendGridMessage('Your SendGrid template ID')) /** * optionally set the from address. * by default this comes from config/mail.from * ->from('no-reply@test.com', 'App name') */ /** * optionally set the recipient. * by default it's $notifiable->routeNotificationFor('mail') * ->to('hello@example.com', 'Mr. Smith') */ ->payload([ "template_var_1" => "template_value_1" ]); } }
toSendGrid
方法将接收一个$notifiable
实体,并应返回一个NotificationChannels\SendGrid\SendGridMessage
实例。
💡除非您明确设置,否则发件人地址将设置为config('mail.from.address')
,而收件人值将是从$notifiable->routeNotificationFor('mail');
返回的值。
沙盒模式
默认情况下,沙盒模式是关闭的。您可以使用setSandboxMode($bool)
方法启用/禁用它。
示例
return (new SendGridMessage('Your SendGrid template ID')) ->setSandboxMode(true) ->payload([ 'template_var_1' => 'template_value_1', 'template_var_2' => [ 'value_1', 'value_2', 'value_3', ], ]);
当启用沙盒模式发送请求时,Sendgrid将验证表单、类型和请求的形状。不会发送任何电子邮件。您可以在此处了解更多关于沙盒模式的信息:这里。
附件
您可以将文件附加到消息中或内嵌(内联附件)。SendGridMessage
对象公开以下方法来帮助您完成此操作
attach($file, $options)
attachData($data, $name, $options)
embed($file, $options)
embedData($data, $name, $options)
值得了解的知识
- 当使用
attachData
和embedData
时,您必须始终在选项数组中传递mime
键。 - 您可以使用选项中的
as
键来更改显示在电子邮件中的文件名。(例如,attach($file, ['as' => 'invoice-3252.pdf'])
) embed
和embedData
方法将返回带有cid:
前缀的ContentID(例如,embed('avatar.jpg') -> "cid:avatar.jpg"
)。
完全访问Sendgrid邮件对象
如果您需要更多自定义选项,您可以直接与底层Sendgrid邮件对象一起工作。要利用此功能,请简单地使用customize
方法传递一个回调。
use SendGrid\Mail\Mail; return (new SendGridMessage('Your SendGrid template ID')) ->payload([ 'template_var_1' => 'template_value_1', 'template_var_2' => [ 'value_1', 'value_2', 'value_3', ], ]) ->customize(function (Mail $mail) { // Send a carbon copy (cc) to another address $mail->addCc('test@test.com'); // Send a blind carbon copy (bcc) to another address $mail->addBcc('bcc@test.com'); });
有关所有选项,您可以在此处查看Sendgrid的邮件类:这里。
访问SendGrid响应
通知发送后,Laravel将触发Illuminate\Notifications\Events\NotificationSent
事件。您可以监听此事件以获取SendGrid响应对象和/或消息ID。
use Illuminate\Notifications\Events\NotificationSent; Event::listen(NotificationSent::class, function (NotificationSent $event) { /** * @var \SendGrid\Response $response */ $response = $event->response; });
变更日志
请参阅变更日志以获取有关最近更改的更多信息。
测试
$ composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件hello@swiftmade.co联系,而不是使用问题跟踪器。
贡献
请参阅CONTRIBUTING以获取详细信息。
致谢
许可
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。