AWS Blog

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:

#!/bin/env php

Create a MIME message with text and HTML bodies, and a PDF attachment:

$mail_mime = new Mail_mime(array(‘eol’ => “\n”));
$mail_mime->setTxtBody(“Hello Jeff, here’s your invoice.\n”);
$mail_mime->setHTMLBody(“<p>Hello Jeff, here’s your invoice.</p>”);
$mail_mime->addAttachment(“/tmp/invoice.pdf”, “application/pdf”);

Retrieve the complete message body in MIME format, along with the headers:

$body = $mail_mime->get();
$headers = $mail_mime->txtHeaders(array(‘From’ => ‘’, ‘Subject’ => “Invoice for Jeff”));

Assemble the message using the headers and the body:

$message = $headers . “\r\n” . $body;

Send the message (which must be base 64 encoded):

$ses = new AmazonSES();
$r = $ses->send_raw_email(array(‘Data’ => base64_encode($message)), array(‘Destinations’ => ‘’));

Report on status:

if ($r->isOK())
  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.

— Jeff;