Search code examples
phplaravelamazon-web-servicesamazon-snsamazon-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).

I would like to set to the ConfigurationSetName to be able to recieve notifications via AWS SNS.

At the moment, I am using the Laravel Mail class using SES and it is working but not the notifications.

Is there any way to do this? Or do I have to use the SesClient always to assign the ConfigurationSetName?


Solution

  • According to the AWS Documentation, you can specify the X-SES-CONFIGURATION-SET header on the email.

    From a Laravel perspective, the easiest way is to specify the headers to be sent right from your 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. 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.

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

    I also verified that it works.