SnapShooter Backups Server, Database, Application and Laravel Backups - Get fully protected with SnapShooter

Sending Email with SES in CakePHP 3

AWS Simple Email Service (SES) provides a reliable and scalable infrastructure for sending email. If you are not yet using an email service yet for your application, we highly recommend AWS. The days of managing your own email infrastructure is long gone.

In this tutorial, we will show you how to set up CakePHP 3 to send email with AWS SES via SMTP. In our opinion, integrating AWS SES with CakePHP 3 by SMTP is more straightforward comparing to API.

Create CakePHP 3 Email Transport Profile

First, we need to create a CakePHP email transport profile for AWS SES. We can create it in CakePHP configuration file, it is typically config/app.php. You can create it in your custom configuration file if you have any.

'EmailTransport' => [
        'ses' => [
            'className' => 'Smtp',
            'host' => 'email-smtp.us-east-1.amazonaws.com',
            'port' => 587,
            'username' => '',
            'password' => '',
            'tls' => true
        ]
]

We have named this transport profile as ses, you can name it whatever you prefer. Let's take a look at those configuration key:

  • className: this key's value should always be Smtp, since we are using SMTP for the integration.
  • host: replace this key's value with your specific AWS region.
  • port: we can use 587 or 2587 for the port. The reason we do not want to use 25 is specified here.
  • username: specify this key's value with your SES SMTP username.
  • password: specify this key's value with your SES SMTP password.
  • tls: this key's value should always be true. This is SES perferred encryption.

Create CakePHP 3 Email Profile

Now we need to create a CakePHP 3 email profile, so that we can use it easily in our application code.

'Email' => [
    'default' => [
        'transport' => 'ses',
        'xxx' => 'xxx'
    ],
],

As you can see, we have given the profile a name default and specify its transport to be ses,which is created in step 1. The rest is our regular email settings such as from, to and so on, which you should already be familiar with.

In case you need to look up the code again, CakePHP 3 email documentation is here.

Working with Google Cloud Engine

If by any chance, your CakePHP 3 application is hosted at Google Cloud Engine and you face an issue with sending email with SES, it is probably caused by the port issue.

According to Google Cloud Engine's documentation. They have blocked outbound connections on ports 25, 465, and 587. That means if you are using port 587 in CakePHP email transport profile, it won't go through. Lucky for us, AWS SES provides an alternative port 2587. That will be our savior.

The end

Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.