简体   繁体   English

使用 Laravel Mail 类通过 AWS SES 发送电子邮件

[英]Send email via AWS SES using Laravel Mail class

I am trying to send email using AWS SES service and the Laravel Mail class ( Illuminate\\Support\\Facades\\Mail ).我正在尝试使用 AWS SES 服务和 Laravel 邮件类 ( Illuminate\\Support\\Facades\\Mail ) 发送电子邮件。

I would like to set to the ConfigurationSetName to be able to recieve notifications via AWS SNS.我想设置为ConfigurationSetName以便能够通过 AWS SNS 接收通知。

At the moment, I am using the Laravel Mail class using SES and it is working but not the notifications.目前,我正在使用 SES 的 Laravel Mail 类,它正在工作,但没有通知。

Is there any way to do this?有没有办法做到这一点? Or do I have to use the SesClient always to assign the ConfigurationSetName ?还是我必须始终使用 SesClient 来分配ConfigurationSetName

According to the AWS Documentation , you can specify the X-SES-CONFIGURATION-SET header on the email. 根据AWS文档 ,您可以在电子邮件上指定X-SES-CONFIGURATION-SET标头。

From a Laravel perspective, the easiest way is to specify the headers to be sent right from your Mailable: 从Laravel的角度来看,最简单的方法是指定要直接从Mailable发送的标头:

public function build()
{
    $this->markdown('emails.creditcard.added');

    $this->withSwiftMessage(function ($message) {
        $message->getHeaders()
                ->addTextHeader('X-SES-CONFIGURATION-SET', '####');
    });
}

You could also do it using the Mail Facade, since it has an underlying Swift_Message instance. 您还可以使用Mail Facade来完成此操作,因为它具有基础的Swift_Message实例。 For example: 例如:

Mail::send('emails.creditcard.added', [], function ($message) use($user) {
    $message->to($user->email)
        ->getSwiftMessage()
        ->getHeaders()
        ->addTextHeader('X-SES-CONFIGURATION-SET', '####');
});

If you want to add this header to all emails, you could also listen to the Illuminate\\Mail\\Events\\MessageSending event and add the headers from there. 如果要将此标头添加到所有电子邮件中,还可以侦听Illuminate\\Mail\\Events\\MessageSending事件并从此处添加标头。

public function handle(MessageSending $event)
{
    $headers = $event->message->getHeaders();
    $headers->addTextHeader('X-SES-CONFIGURATION-SET', '####');
}

I also verified that it works. 我还验证了它的工作原理。

Just want to add on to @Mozammil's answer.只想补充@Mozammil 的回答。

In case you want to add multiple headers:如果您想添加多个标题:

return $this->view('mail-template')
    ->withSwiftMessage(function ($message) {
        $headers = $message->getHeaders();
        $headers->addTextHeader('X-SES-CONFIGURATION-SET', 'my-configuration-set');
        $headers->addTextHeader('ANOTHER-HEADER', 'header-value');
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM