Sending raw email using the Amazon SES API v2
You can use the Amazon SES API v2 SendEmail
operation with the content type
specified as raw
to send customized messages to your recipients using the raw
email format.
About email header fields
Simple Mail Transfer Protocol (SMTP) specifies how email messages are to be sent by
defining the mail envelope and some of its parameters, but it does not concern itself
with the content of the message. Instead, the Internet Message Format (RFC 5322
With the Internet Message Format specification, every email message consists of a header and a body. The header consists of message metadata, and the body contains the message itself. For more information about email headers and bodies, see Email format in Amazon SES.
Using MIME
The SMTP protocol was originally designed to send email messages that only contained 7-bit ASCII characters. This specification makes SMTP insufficient for non-ASCII text encodings (such as Unicode), binary content, or attachments. The Multipurpose Internet Mail Extensions standard (MIME) was developed to make it possible to send many other kinds of content using SMTP.
The MIME standard works by breaking the message body into multiple parts and then specifying what is to be done with each part. For example, one part of an email message body might be plain text, while another might be HTML. In addition, MIME allows email messages to contain one or more attachments. Message recipients can view the attachments from within their email clients, or they can save the attachments.
The message header and content are separated by a blank line. Each part of the email is separated by a boundary, a string of characters that denotes the beginning and ending of each part.
The multipart message in the following example contains a text and an HTML part, and
an attachment. The attachment should be placed just below the attachment headers and is most often
encoded in base64
as shown in this example.
From: "Sender Name" <sender@example.com> To: recipient@example.com Subject: Customer service contact info Content-Type: multipart/mixed; boundary="a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a" --a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a Content-Type: multipart/alternative; boundary="sub_a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a" --sub_a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Please see the attached file for a list of customers to contact. --sub_a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable <html> <head></head> <body> <h1>Hello!</h1> <p>Please see the attached file for a list of customers to contact.</p> </body> </html> --sub_a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a-- --a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a Content-Type: text/plain; name="customers.txt" Content-Description: customers.txt Content-Disposition: attachment;filename="customers.txt"; creation-date="Sat, 05 Aug 2017 19:35:36 GMT"; Content-Transfer-Encoding: base64 SUQsRmlyc3ROYW1lLExhc3ROYW1lLENvdW50cnkKMzQ4LEpvaG4sU3RpbGVzLENhbmFkYQo5MjM4 OSxKaWUsTGl1LENoaW5hCjczNCxTaGlybGV5LFJvZHJpZ3VleixVbml0ZWQgU3RhdGVzCjI4OTMs QW5heWEsSXllbmdhcixJbmRpYQ== --a3f166a86b56ff6c37755292d690675717ea3cd9de81228ec2b76ed4a15d6d1a--
The content type for the message is multipart/mixed
, which indicates that
the message has many parts (in this example, a body and an attachment), and the
receiving client must handle each part separately.
Nested within the body section is a second part that uses the
multipart/alternative
content type. This content type indicates that
each part contains alternative versions of the same content (in this case, a text
version and an HTML version). If the recipient's email client can display HTML content,
then it shows the HTML version of the message body. If the recipient's email client
can't display HTML content, then it shows the plain text version of the message
body.
Both versions of the message also contain an attachment (in this case, a short text file that contains some customer names).
When you nest a MIME part within another part, as in this example, the nested part
must use a boundary
parameter that is distinct from the
boundary
parameter in the parent part. These boundaries should be
unique strings of characters. To define a boundary between MIME parts, type two hyphens
(--) followed by the boundary string. At the end of a MIME part, place two hyphens at
both the beginning and the end of the boundary string.
Note
A message cannot have more than 500 MIME parts.
MIME Encoding
To maintain compatibility with older systems, Amazon SES honors the 7-bit ASCII
limitation of SMTP as defined in RFC 2821
Email addresses
The email address string must be 7-bit ASCII. If you want to send to or from
email addresses that contain Unicode characters in the domain part of an
address, you must encode the domain using Punycode. Punycode is not permitted in
the local part of the email address (the part before the @ sign) nor in the
"friendly from" name. If you want to use Unicode characters in the "friendly
from" name, you must encode the "friendly from" name using MIME encoded-word
syntax, as described in Sending raw email using the Amazon SES API v2. For more information about Punycode, see
RFC 3492
Note
This rule only applies to email addresses that you specify in the message
envelope, not the message headers. When you use the Amazon SES API v2
SendEmail
operation, the addresses you specify in the
Source
and Destinations
parameters define the envelope sender and recipients, respectively.
Email headers
To encode a message header, use MIME encoded-word syntax. MIME encoded word syntax uses the following format:
=?
charset
?encoding
?encoded-text
?=
The value of
can be either
encoding
Q
or B
. If the value of encoding is
Q
, then the value
has to use Q-encoding.
If the value of encoding is encoded-text
B
, then the value of
has to use base64
encoding.encoded-text
For example, if you want to use the string "Як ти поживаєш?" in the subject line of an email, you can use either of the following encodings:
-
Q-encoding
=?utf-8?Q?=D0=AF=D0=BA_=D1=82=D0=B8_=D0=BF=D0=BE=D0=B6=D0=B8=D0=B2=D0=B0=D1=94=D1=88=3F?=
-
Base64 encoding
=?utf-8?B?0K/QuiDRgtC4INC/0L7QttC40LLQsNGU0Yg/?=
For more information about Q-encoding, see RFC 2047
Message body
To encode the body of a message, you can use quoted-printable encoding or
base64 encoding. Then, use the Content-Transfer-Encoding
header to indicate which encoding scheme you used.
For example, assume the body of your message contains the following text:
१९७२ मे रे टॉमलिंसन ने पहला ई-मेल संदेश भेजा | रे टॉमलिंसन ने ही सर्वप्रथम @ चिन्ह का चयन किया और इन्ही को ईमेल का आविष्कारक माना जाता है
If you choose to encode this text using base64 encoding, first specify the following header:
Content-Transfer-Encoding: base64
Then, in the body section of the email, include the base64-encoded text:
4KWn4KWv4KWt4KWoIOCkruClhyDgpLDgpYcg4KSf4KWJ4KSu4KSy4KS/4KSC4KS44KSoIOCkqOCl hyDgpKrgpLngpLLgpL4g4KSILeCkruClh+CksiDgpLjgpILgpKbgpYfgpLYg4KSt4KWH4KSc4KS+ IHwg4KSw4KWHIOCkn+ClieCkruCksuCkv+CkguCkuOCkqCDgpKjgpYcg4KS54KWAIOCkuOCksOCl jeCkteCkquCljeCksOCkpeCkriBAIOCkmuCkv+CkqOCljeCkuSDgpJXgpL4g4KSa4KSv4KSoIOCk leCkv+Ckr+CkviDgpJTgpLAg4KSH4KSo4KWN4KS54KWAIOCkleCliyDgpIjgpK7gpYfgpLIg4KSV 4KS+IOCkhuCkteCkv+Ckt+CljeCkleCkvuCksOCklSDgpK7gpL7gpKjgpL4g4KSc4KS+4KSk4KS+ IOCkueCliAo=
Note
In some cases, you can use the 8bit Content-Transfer-Encoding
in messages that you send using Amazon SES. However, if Amazon SES has to make any
changes to your messages (for example, when you use open and click tracking), 8-bit-encoded
content might not appear correctly when it arrives in recipients' inboxes.
For this reason, you should always encode content that isn't 7-bit
ASCII.
File attachments
To attach a file to an email, you have to encode the attachment using base64 encoding. Attachments are typically placed in dedicated MIME message parts, which include the following headers:
-
Content-Type – The file type of the attachment. The following are examples of common MIME Content-Type declarations:
-
Plain text file –
Content-Type: text/plain; name="sample.txt"
-
Microsoft Word Document –
Content-Type: application/msword; name="document.docx"
-
JPG image –
Content-Type: image/jpeg; name="photo.jpeg"
-
-
Content-Disposition – Specifies how the recipient's email client should handle the content. For attachments, this value is
Content-Disposition: attachment
. -
Content-Transfer-Encoding – The scheme that was used to encode the attachment. For file attachments, this value is almost always
base64
. -
The encoded attachment – You must encode the actual attachment and include it in the body below the attachment headers as shown in the example.
Amazon SES accepts most common file types. For a list of file types that Amazon SES doesn't accept, see Amazon SES unsupported attachment types.
Sending raw email using the Amazon SES API v2
The Amazon SES API v2 provides the SendEmail
action, which lets you compose and
send an email message in the format that you specify when you set the content type to
either simple, raw, or templated. For a complete description, see SendEmail
. The following example will specify the content
type as raw
to send a messages using the raw email format.
Note
For tips on how to increase your email sending speed when you make multiple calls
to SendEmail
, see Increasing throughput with Amazon SES.
The message body must contain a properly formatted, raw email message, with appropriate header fields and message body encoding. Although it's possible to construct the raw message manually within an application, it's much easier to do so using existing mail libraries.