Sending e-mails from CakePHP 2.0 using Amazon’s SES SMTP server

In mid-December 2011, Amazon announced that they simplified the process of using Amazon SES by allowing users to send e-mails through a SMTP server. You can read some details about Amazon SES and an alternate implementation in CakePHP 1.3 here.

What follows is a simple setup to make CakePHP 2.0 send e-mails through Amazon SES:

Step 1: Creating your SMTP credentials

Using the Amazon SES console you will be able to create unique credentials for your app (both user and password). These are not the same as the key or secret used to access the API. To do this, go to your SES console and click on SMTP Settings. A new page will open with partial information (such as the server address and the port number you need to use). At the bottom of the page, you can find a button called “Create my SMTP Credentials”. Click on it and choose your username (this can be anything and it is used to create an encrypted string that will serve as the actual username). After clicking the “Create” button you will be served your credentials. These will be shown to you only once and you cannot come back to them or modify them. You can, however, download them.

Step 2: CakePHP configuration

We will be using Cake’s update e-mail component for our setup.

First, if you haven’t done so, you need to rename the email.php.default file under /app/Config to email.php. Once you’ve done this, open the file and go the second configuration array, called “Smtp” and the following values:

	public $smtp = array(
		'transport' => 'Smtp',
		'from' => array('' => 'Testing'), //this needs to validated with SES. More details here:
		'host' => 'ssl://',
		'port' => 465,
		'timeout' => 10,
		'username' => 'YOUR-GENERATED-USERNAME',
		'password' => 'YOUR-GENERATED-PASSWORD',
		'client' => null,
		'log' => false

Please notice the “ssl://” before the actual hostname. That is the key to open the right channel to communicate with the SMTP server

Second, we have the controller configuration. This is a basic configuration that can be modified per your needs. Details about modifying it here.

	App::uses('CakeEmail', 'Network/Email');
	class FrontendController extends AppController {
	    public $name = 'Frontend';
		function index() {
			$email = new CakeEmail();
				->template('test_template', 'test_layout') //I'm assuming these were created
			    ->from(array('' => 'Example'))
			    ->subject('I\'m just testing something')

Step 3: Never worry about your e-mails again