Sending email from AWS in PHP with AmazonSES
Posted: April 28th, 2011 | Author: Alex Korn | Filed under: AWS, Hosting, Tutorials | Tags: PHP | 2 Comments »Update 2013/06/27: I’ve updated some of the code to match new SES interfaces.
For those who have sent emails in PHP, the following code should look quite familiar:
mail($to, $subject, $message); |
Unfortunately, this will silently fail on Amazon Web Services. But you do need to send those emails, right? Well, it’s pretty easy to get emails sent from AWS with Amazon Simple Email Service (Amazon SES). However, in my research on this, I found several false leads and no good tutorials, so here goes: step by step, how to send emails with AWS.
First step: sign up.
AWS SDK for PHP
Next, install the source code code for AWS SDK for PHP. Run the following in the directory where you store external libraries (using sudo
when needed):
yum install git git clone git://github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP
Now that you have the the AWS SDK on your server, include AWSSDKforPHP/sdk.class.php
into your application. You now have access to the AmazonSES
class.
The following is a basic function to emulate PHP’s mail()
.
function amazonSesEmail($to, $subject, $message) { $amazonSes = new AmazonSES(array( 'key' => AWS_KEY, 'secret' => AWS_SECRET_KEY )); $response = $amazonSes->send_email(AWS_SES_FROM_EMAIL, array('ToAddresses' => array($to)), array( 'Subject.Data' => $subject, 'Body.Text.Data' => $message, ) ); if (!$response->isOK()) { // handle error } } |
Note that you need to define the AWS_KEY and AWS_SECRET_KEY for your application. You can find these by going to your AWS Security Credentials page and looking under “Access Keys”.
Validating the sending email address
Next, define AWS_SES_FROM_EMAIL
to be the email address from which you want to send emails, e.g. no-reply@example.com
. If you try to send an email at this point, you’ll get a MessageRejected
error with the message “Email address is not verified.” Several pages I visited recommended I download the Amazon SES Scripts (Perl), but don’t do it! You’ll need to subject yourself to a world of pain in the form of installing Perl modules* (and probably Perl too, given that you’re reading a post on PHP!).
Instead, you can actually use AmazonSES
to verify email addresses in PHP.
$amazonSes = new AmazonSES(array( 'key' => AWS_KEY, 'secret' => AWS_SECRET_KEY )); $amazonSes->verify_email_address($fromEmailAddress); |
Much easier than the Perl script shenanigans. This will send an email to your sending email address with a link to verify that you own that email address. Do the same for your recipient addresses (since you’re in sandbox mode, which has restrictions including the requirement to validate your recipient address). Verify each address and you should be good to go to send emails.
Setting up production access
At this point you’ll be able to send a couple test emails, but the volume is fairly limited. Request production access and you should be granted the ability to send many more emails and not need to verify recipient email addresses.
Reference
Visit the AWS SDK for PHP reference page for much more information on how AWS SDK for PHP works.
Amazon SES Scripts (Perl)
If you do want to install the Amazon SES Perl scripts and are getting an error along the lines of “Can’t locate XML/LibXML.pm in @INC…”, you can use yum
to install all of the necessary packages to get them to run.
yum -y install perl-Digest-SHA perl-URI perl-libwww-perl perl-MIME-tools perl-Crypt-SSLeay perl-XML-LibXML
Thanks to this post for that hint.
Great post. One point to note is that when in sandbox and not production mode, each recipient also needs their email address verified.
Good call, Stuart. I’ve added that to the post. Thanks!