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('no-reply@example.com' => 'Testing'), //this needs to validated with SES. More details here: http://viorel.sfetea.ro/blog/en/2011/10/31/sending-e-mails-the-efficient-way-using-amazon-ses-and-cakephp/
		'host' => 'ssl://email-smtp.us-east-1.amazonaws.com',
		'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.

<?php
	App::uses('CakeEmail', 'Network/Email');
 
	class FrontendController extends AppController {
	    public $name = 'Frontend';
 
		function index() {
			$email = new CakeEmail();
			$email->config('smtp')
				->template('test_template', 'test_layout') //I'm assuming these were created
			    ->emailFormat('html')
			    ->to('anyone@example.com')
			    ->from(array('no-reply@example.com' => 'Example'))
			    ->subject('I\'m just testing something')
			    ->send();
		}
	}
?>

Step 3: Never worry about your e-mails again

10 thoughts on “Sending e-mails from CakePHP 2.0 using Amazon’s SES SMTP server

    • Hello. Glad I helped. The part about the address verification is covered in the detailed article cited in the first paragraph, but I added an extra reference just to make it more clear

  1. I integrated the code in cakephp 2.0 . I got a error like (SMTP Error: 554 Message rejected: Email address is not verified.) when i send the mail. Please can anyone help me to solve this.

  2. Invalid email: "site@localhost"
    Error: An Internal Error Has Occurred.
    Stack Trace
    CORE/Cake/Network/Email/CakeEmail.php line 567 → CakeEmail->_setEmail(string, array, null)
    CORE/Cake/Network/Email/CakeEmail.php line 353 → CakeEmail->_setEmailSingle(string, array, null, string)
    CORE/Cake/Network/Email/CakeEmail.php line 1141 → CakeEmail->from(array)
    CORE/Cake/Network/Email/CakeEmail.php line 1032 → CakeEmail->_applyConfig(string)
    APP/Controller/ImagesController.php line 177 → CakeEmail->config(string)
    [internal function] → ImagesController->index()
    CORE/Cake/Controller/Controller.php line 485 → ReflectionMethod->invokeArgs(ImagesController, array)
    CORE/Cake/Routing/Dispatcher.php line 186 → Controller->invokeAction(CakeRequest)
    CORE/Cake/Routing/Dispatcher.php line 161 → Dispatcher->_invoke(ImagesController, CakeRequest, CakeResponse)
    APP/webroot/index.php line 92 → Dispatcher->dispatch(CakeRequest, CakeResponse)
    why?please tell me

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">