Simple Email in the Cloud
In this article, I will explain how Amazon’s Simple Email Service (SES) is a better way to send email from your web application, and I’ll provide a roadmap for getting started.
Generally speaking, email on the internet is sent using SMTP servers. Web applications are no exception. But, as anyone who has dealt with large-scale email sending through their web application knows, using SMTP to send email can be a real pain.
The easiest way to see how Amazon’s SES service improves upon the traditional methods of sending email is to compare SES to SMTP:
|Feature||Traditional SMTP||Amazon SES|
|Availability||Either you or your web host must maintain an SMTP server. If the server is down, your application cannot send email.||Unlike a traditional SMTP server that is a single point of failure, Amazon SES is distributed and designed to be highly available.|
|Scalability||As your web application sends more and more email, you’ll need to upgrade your SMTP server. Eventually you’ll need multiple servers in a load-balancing or clustering configuration.||Amazon SES can easily handle the growth of your email volume as your application expands. No need to invest in servers, setup, or maintenance. You simply pay as you go for what you use.|
|Cost||Sure, you could try and set up your own scalable, highly-available SMTP platform. All you’ll need is a few servers at multiple datacenters and two or three fulltime technicians to monitor and maintain the service. That’s in your budget, right?||You can use Amazon SES for free—up to 2,000 messages per day (you just pay for traffic). If you send more than that, you pay mere pennies to send thousands of emails.|
|Deliverability||Major email providers strictly filter incoming mail. In order to achieve high delivery rates, you must be able and willing to jump through all kinds of technical hoops.||With SES, Amazon takes steps to ensure your messages are delivered.|
|Tracking||SMTP server logs can tell you if your email was accept by the receiving ISP (have fun working with server logs to track delivery).||Amazon SES provides easy access to data about email delivery. Not only can you find out if your email is bouncing, but with SES you can see how many of your emails are being marked as spam or junk by recipients at major ISPs.|
Now that you know how much better life can be using Amazon SES in your application, let’s dive into a few examples. We’ll be building off knowledge from a previous article titled “Getting Started with the Amazon AWS PHP SDK”.
Before you can work with any of the following examples, you must sign up for the Amazon SES service. To do so, simply sign in to your AWS account, visit the Amazon SES product page (http://aws.amazon.com/ses/), and click “Sign up now”.
Sandbox Limitations and Email Verification
The first thing you need to know about using Amazon SES is that, when you first sign up for the service, your account will be limited in functionality. This means two things:
- You can only send from email addresses that you have verified.
- You can only send to email addresses you have verified.
This obviously won’t work for a production deployment, but it’s just fine for testing. So the first thing you’re going to need to do is verify a couple of email addresses. You can start the verification process with just a few lines of code:
// You'll need the SDK Class for these examples. // Learn more at: http://www.sitepoint.com/getting-started-with-the-aws-php-sdk/ require_once 'sdk.class.php'; // Create an instance of the SES class. $email = new AmazonSES(); // Call the verification method using your own email address $response = $email->verify_email_address('firstname.lastname@example.org'); print_r($response);
Simply running the code example above will initiate verification. You’ll receive an email at the address you submit with a coded verification link. Then just click the link and your email address will be verified.
Repeat this process for any number of email addresses you’d like to send from or to during your testing.
Sending Your First Email
Now that you have a verified email address or two, you can start sending email. This is extremely simple:
require_once 'sdk.class.php'; $email = new AmazonSES(); // Your verfified sending email address. $sendFrom = 'email@example.com'; // The email address you're sending to // (must be a verified email while your account // is in the sandbox). $sendTo = 'firstname.lastname@example.org'; // The message and body of your email: // (Adding .date('c') to the subject // makes it easy to track separate tests) $messageSubject = 'My First AWS Email: '.date('c'); $messageBody = 'Hi there! This is my first message.'; // Create the request: $response = $email->send_email( $sendFrom, array('ToAddresses' => $sendTo), array( 'Subject.Data' => $messageSubject, 'Body.Text.Data' => $messageBody ) ); // If your send fails, // You'll be able to find the reason why // the response information. print_r($response);
That’s all there is to it!
Sending HTML Email
Adding HTML to your email makes it easy to add formatting and links that can make your email more effective. The PHP SDK makes it easy:
require_once 'sdk.class.php'; $email = new AmazonSES(); $sendFrom = 'email@example.com'; $sendTo = 'firstname.lastname@example.org'; $messageSubject = 'My SES Email with HTML: '.date('c'); $messageBody = 'Hi there! This is a message with a link: http://www.sitepoint.com'; // You can specify an HTML alternative // for your email which will allow you to // add links, formatting, tables, // and other html elements to your message: $messageBodyHtml = 'Hi there! This is a message with a link: <a href="http://www.sitepoint.com" style="color: #D90000;">http://www.sitepoint.com</a>'; $response = $email->send_email( $sendFrom, array('ToAddresses' => $sendTo), array( 'Subject.Data' => $messageSubject, 'Body.Text.Data' => $messageBody, 'Body.Html.Data' => $messageBodyHtml, ) ); print_r($response);
More Advanced Options
In addition to the common task of sending formatted email, the SDK provides some additional methods that can be very useful:
Send Raw Email – For more advanced functionality, the PHP SDK allows you to send a raw email message. This allows you to add multiple parts to your email, including attachments.
List Verified Email Addresses – It can be handy to get a list of email addresses you have previously verified, and in some cases you might even want to us the “Deleted Verified Email Address” method to remove emails that should no longer be allowed to send from your account.
Get Send Quota – Based on your email habits, your Amazon AWS account will place a limit on how many emails you can send per day. This limit can range from 1,000 emails up to a million or more per day.
Get Send Statistics – This powerful method allows you to track real-time statistics, including Successful Delivery Attempts, Rejected Messages, Bounces, and Complaints.
Leaving the Sandbox
Once you’re ready to go live with your new email service, you’ll need to request production access for sending email. A link to make this request can be found on the Amazon SES product page and requests are usually granted within a day or so.
A Few More Tips
Abuse – Amazon SES is not designed to make it easy to send unsolicited email to mailing lists you buy. If you send unwanted email using SES, you’ll be shut down very quickly.
Quota – Amazon SES dynamically determines per-day sending limits. The best way to increase your limit is to send high-quality emails (low bounce rates and complaints). Your limit starts out at 1,000 emails, but it will increase within a few days if you constantly send up to 1,000 high quality emails per day.
Limits – Messages cannot be larger than 10MB. If you need to send larger files, I would recommend using Amazon S3 service with secure, time-restricted links rather than attaching large files. Emails are also limited to 50 recipients (combined total of to, cc, and bcc).
DNS Setup – To maximize delivery rates, Amazon recommends setting up special DNS records that tell other ISPs that your domain authorizes Amazon to send email on your behalf. To learn more, read the “SPF, Sender ID, and DKIM” sections in the Amazon SES documentation.
Switching my transactional and other system-generated email to Amazon SES has greatly reduced the amount of time, effort, and money that I need to put into maintaining reliable email service in my web applications. And it’s great to know that as my applications grow, I don’t need to worry about outgrowing my email service.
Have questions or suggestions? Post comments below!