Amazon Simple Email Service provides a simple way to send e-mails without having to maintain your own mail server. This PHP class is a REST-based interface to that service.
A bit of example code will be a good demonstration of how simple this class is to use. Please read through these examples, and then feel free leave a comment if you have any questions or suggestions.
Update: Daniel Zahariev has forked the project and implemented the missing SendRawEmail functionality. You can download it at his github project page.
First, you’ll need to create a SimpleEmailService class object:
require_once('ses.php'); $ses = new SimpleEmailService('Access Key Here', 'Secret Key Here');
If this is your first time using Simple Email Service, you will need to request verification of at least one e-mail address, so you can send messages:
print_r($ses->verifyEmailAddress('firstname.lastname@example.org')); ------- Array ( [RequestId] => 1b086469-291d-11e0-85af-df1284f62f28 )
Every request you make to SimpleEmailService will return a request id. This id may be useful if you need to contact AWS about any problems. For brevity, I will omit the request id if it is the only value returned from a service call.
After you’ve requested verification, you’ll get an e-mail at that address with a link. Click the link to get your address approved. Once you’ve done that, you can use it as the ‘From’ address in the e-mails you send through SES. If you don’t have production access yet, you’ll also need to request verification for all addresses you want to send mail to.
If you want to see what addresses have been verified on your account, it’s easy:
print_r($ses->listVerifiedEmailAddresses()); ------- Array ( [RequestId] => 77128e89-291d-11e0-986f-43f07db0572a [Addresses] => Array (  => email@example.com  => firstname.lastname@example.org ) )
Removing an address from the verified address list is just as easy:
This call will return a request id if you need it.
The only thing left to do is send an e-mail, so let’s try it. First, you’ll need a SimpleEmailServiceMessage object. Then, you’ll want to set various properties on the message. For example:
$m = new SimpleEmailServiceMessage(); $m->addTo('email@example.com'); $m->setFrom('firstname.lastname@example.org'); $m->setSubject('Hello, world!'); $m->setMessageFromString('This is the message body.'); print_r($ses->sendEmail($m)); ------- Array ( [MessageId] => 0000012dc5e4b4c0-b2c566ad-dcd0-4d23-bea5-f40da774033c-000000 [RequestId] => 4953a96e-29d4-11e0-8907-21df9ed6ffe3 )
And that’s all there is to it!
There are a few more things you can do with this class. You can make two informational queries, try these out:
For brevity I will not show the output of those two API calls. This information will help you keep track of the health of your account. See the Simple Email Service documentation on GetSendQuota and GetSendStatistics for more information on these calls.
You can set multiple to/cc/bcc addresses, either individually or all at once:
$m->addTo('email@example.com'); $m->addTo(array('firstname.lastname@example.org', 'email@example.com')); $m->addCC('firstname.lastname@example.org'); $m->addCC(array('email@example.com', 'firstname.lastname@example.org')); $m->addBCC('email@example.com'); $m->addBCC(array('firstname.lastname@example.org', 'email@example.com'));
These calls are cumulative, so in the above example, three addresses end up in each of the To, CC, and BCC fields.
You can also set one or more Reply-To addresses:
$m->addReplyTo('firstname.lastname@example.org'); $m->addReplyTo(array('email@example.com', 'firstname.lastname@example.org'));
You can set a return path address:
You can use the contents of a file as the message text instead of a string:
$m->setMessageFromFile('/path/to/some/file.txt'); // or from a URL, if allow_url_fopen is enabled: $m->setMessageFromURL('http://example.com/somefile.txt');
If you have both a text version and an HTML version of your message, you can set both:
Or from a pair of files instead:
$m->setMessageFromFile($textfilepath, $htmlfilepath); // or from a URL, if allow_url_fopen is enabled: $m->setMessageFromURL($texturl, $htmlurl);
Remember that setMessageFromString, setMessageFromFile, and setMessageFromURL are mutually exclusive. If you call more than one, then whichever call you make last will be the message used.
Finally, if you need to specify the character set used in the subject or message:
The default is UTF-8 if you do not specify a charset, which is usually the right setting. You can read more information in the SES API documentation.
My version of this library does not support the SendRawEmail call, which means you cannot send emails with attachments or custom headers; however, Daniel Zahariev has forked the project and implemented this functionality. You can download it at his github project page.
Disclaimer: Although I work for Amazon, this class was neither produced, endorsed nor supported by Amazon. I wrote this class as a personal project, just for fun. While I no longer maintain it, I am happy to help resolve issues encountered while trying to use it.