Sending e-mails the efficient way using Amazon SES and CakePHP

A few words about Amazon SES

Amazon SES is an e-mail service provided by Amazon that allows professional e-mail sending through Amazon’s servers. It handles sending and processing e-mails so you don’t have to (sending, catching bounces, verifying addresses). I am using SES to ensure the best deliverability rates for notifications e-mails from my apps.

Is that free?

Not really, but it’s worth every cent. And speaking of cents, you probably won’t be spending too many of those using SES. Prices start from 10 cents per 1000 sent e-mails. Rates are even lower if you are using their cloud solutions to host your app.

Let’s get cooking

First, let’s take care of the CakePHP implementation:

Download the PHP SDK Amazon prepared from here, rename the folder to aws-sdk and upload it to your /app/vendors/ folder in the CakePHP installation.

After copying the PHP SDK, you will need a simple component that extends Cake’s email component to use SES. You can get that from my github page (this a tweaked version of an excelent component from Lucas Ferreira). Copy that to your /app/ folder preserving the folder structure.

Assuming you don’t already own an Amazon AWS (that includes SES) account, please create one here. Since this is not a free service, you will need to provide your credit card number before finishing. It will take a few minutes to activate all the services.

Having got all your services activated, you will need to go back to the AWS PHP SDK you have in your CakePHP installation and create the configuration file. Rename the file config-sample.inc.php to config.inc.php and add your own values for AWS_KEY, AWS_SECRET_KEY, AWS_ACCOUNT_ID and AWS_CANONICAL_ID. You will find these numbers on the same page, in the Security Credentials section of your account page.

Testing

Amazon SES requires production access in order to be used freely. This happens because the object here is a very sensitive one and they need to handpick anyone that is going to use their service. This prevents spammers from taking advantage of SES and using it for illegal purposes.

However, you will have sandbox access, meaning that you will be able to use the service fully, but only with addresses you previously define (verify). You will have to verify both the addresses you send e-mail from (this will be the case in production, too) and the e-mail addresses that receive the e-mail. Once you are granted production access, you won’t need to verify the addresses you are sending e-mail to anymore. The verifying process consists of sending a request to the desired address and then clicking a link in an e-mail sent to that address.

You can verify e-mail addresses by calling a method in the EmailService component you previously downloaded from github:

class PagesController extends AppController {
 
	var $name = 'Pages';
	var $components = array('EmailService');
 
	function beforeFilter() {
 
	}
 
	function display() {
		$verify = $this->EmailService->verifyEmailAddress('me@example.com');
		var_dump($verify);
	}
 
}

In relation with the original CakePHP e-mail component, the usage is pretty much the same. In addition, you will just have to the the Email component which kind of e-mails to send. Everything else is the same, just that you have to use $this->EmailService instead of $this->Email. An example:

class PagesController extends AppController {
 
	var $name = 'Pages';
	var $components = array('EmailService');
 
	function display() {
		$this->EmailService->delivery = 'aws_ses'; //or aws_ses_raw
		$this->EmailService->to = array('me@example.com');
		$this->EmailService->subject = 'Example e-mail';
		$this->EmailService->from = 'Me <meagain@example.com>';
		$this->EmailService->template = 'template';
		$this->EmailService->sendAs = 'html';
		$this->EmailService->send();
	}
 
}

aws_ses or aws_ses_raw delivery?

This depends on how your actual e-mail is built. After doing some tests, I ended up to the conclusion that aws_ses is suitable for plain text and light-weight HTML e-mails (no images) and aws_ses_raw is best suited for complex HTML e-mails (text and images) and/or e-mails containing attachments.

…and finally. Requesting production access

Go to this simple form and fill your details in there. You will be receiving production access as soon as they review your request.

5 thoughts on “Sending e-mails the efficient way using Amazon SES and CakePHP

  1. Hi Viorel Sfetea,

    I am using the EmailService component to send an email. But i faced one error
    Class "_aws_sesTransport" not found. when i send the email.

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="">