Free PHP Class for Sending Email Through Amazon SES
Ever since Amazon launched their "Simple Email Service" (SES), I've been wanting to try it out. Yesterday, I finally got around to sending my first test message. I used a PHP class created by Dan Myers based on one created by Donovan Schonknecht. But I wasn't quite satisfied with it.
The biggest shortcoming is that it doesn't support the "SendRawEmail" API call, which means that it can't send emails with attachments.
So, being a programmer (one who's worked on an email client and a spam filter in past lives), I went to work and added that feature (plus some little performance optimizations, support for adding arbitrary email headers, features designed to make it more useful with large mailing lists, etc.)
And here it is, free to download: Amazon SES PHP class.
For information about how to use it, first visit Dan's page. Then read on to see the features I added.
How to Use the SendRawEmail Feature
Here's an example of how to send an email with a JPEG image attached:
require_once('ses.php');
$ses = new SimpleEmailService('YourAccessKeyIDGoesHere', 'YourSecretKeyGoesHere');
$m = new SimpleEmailServiceMessage();
$m->setFrom('sender-address@example.com');
$m->addTo('recipient-address@example.com');
$m->setSubject('This is a Test');
$m->addRawMessageFromString('text', 'This is the text message', 'text/plain');
$m->addRawMessageFromString('html', '<b>This <i>is</i> the HTML</b> message', 'text/html');
$m->addRawMessageFromFile('attachment1', 'my-picture.jpeg', 'image/jpeg');
$ses->sendEmail($m);
A few important notes:
- If you call "addRawMessageFrom*" functions, any messages you added using the old "setMessageFrom*" functions will be ignored. So be sure to add your text and/or HTML messages using the "addRawMessageFrom*" functions.
- If you want to send the same message to multiple people, and you want each person to see only their own address in the "To" header (ie. you don't want to reveal everyone's email address to everyone else, but you don't want to put them in BCC headers), you can do so by removing any the old recipients, adding any new ones, and sending again. For example:
require_once('ses.php');
$ses = new SimpleEmailService('YourAccessKeyIDGoesHere', 'YourSecretKeyGoesHere');
$m = new SimpleEmailServiceMessage();
$m->setFrom('sender-address@example.com');
$m->addTo('recipient-address@example.com');
$m->setSubject('This is a Test');
$m->setMessageFromString('This is the message body.');
$ses->sendEmail($m);
$m->clearTo();
$m->addTo('next-recipient-address@example.com');
$ses->sendEmail($m); - Taking it a little further, when using the SendRawEmail method, if the message body includes personalization, you can replace it by calling one of the "addRawMessageFrom*" functions with the same first parameter. (When sending messages with attachments to many recipients, this improves performance because any attachments only have to be processed once to prepare them for transmission.) Here's an example (note that you don't have to remove the old message -- since the first parameter is "text" both times addRawMessageFromString is called, the second one overwrites the first):
require_once('ses.php');
$ses = new SimpleEmailService('YourAccessKeyIDGoesHere', 'YourSecretKeyGoesHere');
$m = new SimpleEmailServiceMessage();
$m->setFrom('sender-address@example.com');
$m->addTo('recipient-address@example.com');
$m->setSubject('This is a Test');
$m->addRawMessageFromString('text', 'Hey Joe, what do you know?', 'text/plain');
$m->addRawMessageFromFile('attachment1', 'my-picture.jpeg', 'image/jpeg');
$ses->sendEmail($m);
$m->clearTo();
$m->addTo('next-recipient-address@example.com');
$m->addRawMessageFromString('text', 'Hey Curly, what up dude?', 'text/plain');
$ses->sendEmail($m); - I came across one surprising limitation imposed the the SES service: you can't attach ZIP files to emails. When the service first launched, amazingly, it didn't support attachments at all. Now it supports a pretty good variety of types (you can find a list here). But ZIP isn't among them. Hopefully Amazon will add support for it later. Note that both the MIME type and the filename of the attachment must match what's shown in the documentation for the file to be accepted.
With this piece in place, I'm ready to start building my own custom mailing list management system that I can add features to to my heart's content.
The problem with being a programmer is that you know what's possible, so when you're using a service provider that doesn't do what you want, you're always tempted to sink a bunch of time into rolling your own custom solution.
The good thing about being a programmer is that if you do a good enough job of it, you end up with a product you can sell (which is what happened for me with CaRP Evolution, ExPop, SEO Content Factory...and so on. We'll see whether that's what comes of this effort.
October 21st, 2011 at 6:02 pm
Thanks Antone, any chance of doing a vid on how to set this up? for us non programer types ;-)
October 22nd, 2011 at 6:37 pm
Jeremy,
Since this is just a library to be used by other scripts, it wouldn't be much use to a non-programmer until somebody writes a script to use it, at which point, what you'd need is to know how to set up that script. So I'm not quite sure what I'd put in a video.
October 22nd, 2011 at 6:39 pm
I just uploaded a new version that doesn't base-64 encode text parts unless they contain 8-bit characters or have lines longer than 977 characters (which causes problems with SMTP).
October 30th, 2011 at 5:37 pm
Looking forward to see how this project pans out. Been looking into Interspire, ActiveCampaign, and Octeth Oempro. Surprisingly the last two still don't officially support SES integration.
November 7th, 2011 at 12:23 am
Thanks for your post it helped me a lot. :)
October 1st, 2012 at 1:39 pm
Thanks for this SES improvement, I'm going to use this library with my projects. But, how about inline images, do you have any idea how to implement that?
October 4th, 2012 at 5:19 am
I am geting this error plz help me
Notice: Undefined property: SimpleEmailServiceRequest::$resource in C:\wamp\www\TestApp\ses.php on line 491
Warning: SimpleEmailService::verifyEmailAddress(): 60 SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in C:\wamp\www\TestApp\ses.php on line 357
SMTP = email-smtp.us-east-1.amazonaws.com
; http://php.net/smtp-port
smtp_port = 25, 465 or 587
October 4th, 2012 at 6:29 am
Warning: SimpleEmailService::sendEmail(): Sender - MessageRejected: Email address is not verified. Request Id: 9747a9ff-0e1e-11e2-b3c6-dde140a94cea in C:\wamp\www\TestApp\ses.php on line 363
October 4th, 2012 at 7:18 am
Krishnaveni, it looks like you need to sign into the SES control panel and verify the email address you're using to send messages. SES requires that all sender email addresses be verified to ensure that you don't send email claiming to be from someone else.
October 9th, 2012 at 2:23 am
wher to login in SES i have to send email to daily users i am getting this error
Notice: Undefined property: SimpleEmailServiceRequest::$resource in C:\wamp\www\TestApp\ses.php on line 492
Warning: SimpleEmailService::sendEmail(): 60 SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in C:\wamp\www\TestApp\ses.php on line 358
July 5th, 2013 at 6:34 am
Hi,
Now Amazon SES supports ZIP files :-)
http://docs.aws.amazon.com/ses/latest/DeveloperGuide/mime-types.html