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.
Table Of Content
- Create CakePHP 3 Email Transport Profile
- Create CakePHP 3 Email Profile
- Working with Google Cloud Engine
- The end
1. 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.
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.
2. 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.
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.
3. 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.
4. 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.