Configure Amazon SES on Laravel 5.8 in 5 minutes


Amazon SES (for Amazon Simple Email Service) is the cloud-based email sending service by AWS.

Laravel allows the use of Amazon SES through the Amazon AWS SDK for PHP.

I did not find a lot of documentation and tutorials when I first installed Amazon SES on a Laravel for a client.

Here is a quick tutorial to integrate SES in 5 minutes top chrono!

1 — Create an AWS Account (if you do not have one yet)

It is happening here: https://portal.aws.amazon.com/billing/signup#/start

2 — Add a new domain

The links of this tutorial are for the region eu-west-1, you can use the region you want.

Go here: https://eu-west-1.console.aws.amazon.com/ses/home?region=eu-west-1/verified-senders-domain

and click on Verify a New Domain:

Add a new domain

Indicate your domain and tick Generate DKIM Settings.

To complete verification of your domain, you must add TXT record to the domain’s DNS settings.

There is normally:

  • 1 TXT field
  • 3 CNAME fields
  • 1 MX field

Find this information in the provider’s DNS settings of your domain.
Once done, you will receive an email telling you that your domain has been validated and its “Verification Status” will change to “verified”.

3 — Add an email address

Add the email address you would like to use to send emails here: https://eu-west-1.console.aws.amazon.com/ses/home?region=eu-west-1#verified-senders-email:

You will receive a confirmation email with a link to click to confirm that the email address belongs to you.

4 — Create an IAM user and API keys

To avoid using the API keys of your main account (which has far too permissive rights) we will create an IAM user who will have rights only for Amazon SES.

Go here : https://console.aws.amazon.com/iam/home?region=eu-west-1#/users

Click on “Add user”.

Add IAM user

Enter a name for your user, eg “domain_SES” and tick “Programmatic access”.
Click on “Next”.

On the next screen click on “Attach existing policies directly” and search for the “AmazonSESFullAccess” policy:

Search the AmazonSESFullAccess policy

Tick the line and click “Next”.
Click again “Next” (you can add tags if you want) and then click “Create User”.

Important: note the Access key ID and the Secret access key (click show to display the secret key).

5 — Configure the Laravel

Let’s start by installing the AWS SDK for PHP:

composer require aws/aws-sdk-php

In config/services.php,

'ses' => [
'key' =>
env('SES_KEY'),
'secret' =>
env('SES_KEY_SECRET'),
'region' =>
env('SES_REGION'),
],

In your .env file :

SES_KEY=key_from_AWS
SES_KEY_SECRET=secret_key_from_AWS
SES_REGION=region_from_AWS

In my case the region is eu-west-1.

Run php artisan cache:clear to take into account the changes.

6 — Test of an email sending

Let’s generate a Laravel Mailable :

php artisan make:mail TestAmazonSes

You should have a app/Mails/TestAmazonSes.php file now.

Add this content to it:

TestAmazonSes.php

as you can see with ->view('emais.tpl') it uses a view.

Let’s create this view in resources/views/emails/tpl.blade.php :

Here is a basic responsive template insipired from https://github.com/leemunroe/responsive-html-email-template for our test (you can use the template you want):

tpl.blade.php

Now we juste have to send the email where we want. I will create a route /test to test that everything works well.

In web/routes.php add :

Route::get('test', function () {
Mail::to('email@doe.com)->send(new TestAmazonSes('It works!'));
});

You should receive the email on the indicated address 🙂

Feel free to ask me your questions in the comment section.

via Laravel News Links
Configure Amazon SES on Laravel 5.8 in 5 minutes