Amazon Simple Email Service Now Supports Attachments
You can now use the Amazon Simple Email Service to send email message that include attachments such as images or documents.
There are no new “attachment” APIs. Instead, you simply use the existing SendRawEmail function to send a message that includes one or more MIME parts. Each part of the message must be of a MIME type that is supported by SES. Document and image formats are supported; executable files and archives are not. Consult the SES documentation for a complete list of supported MIME types.
Messages that include attachments incur an additional cost of $0.12 per GB of attachment data. This is in addition to the $0.10 cost for every 1000 messages and the usual cost for outbound data transfer (see the SES page for details).
I spent a few minutes putting together some PHP code to create and send a message with an attachment. I installed and configured the latest version of the AWS SDK for PHP, the Mail_Mime package, and wrote a little bit of code. Here’s what I ended up with:
Load the MIME package and the AWS SDK for PHP:
Create a MIME message with text and HTML bodies, and a PDF attachment:
$mail_mime->setTxtBody(“Hello Jeff, here’s your invoice.\n”);
$mail_mime->setHTMLBody(“<p>Hello Jeff, here’s your invoice.</p>”);
Retrieve the complete message body in MIME format, along with the headers:
$headers = $mail_mime->txtHeaders(array(‘From’ => ‘firstname.lastname@example.org’, ‘Subject’ => “Invoice for Jeff”));
Assemble the message using the headers and the body:
Send the message (which must be base 64 encoded):
$r = $ses->send_raw_email(array(‘Data’ => base64_encode($message)), array(‘Destinations’ => ‘email@example.com’));
Report on status:
print(“Mail sent; message id is ” . (string) $r->body->SendRawEmailResult->MessageId . “\n”);
print(“Mail not sent; error is ” . (string) $r->body->Error->Message . “\n”);
Close up shop:
As you can see, this is pretty straightforward. Per the usual operating protocol for SES, you’ll need to verify the email addresses that you use for testing, and you’ll need to request and receive production access in order to start sending to arbitrary addresses.