How to send attachment using AWS SES in NodeJS?

Viewed 4849

As per the given doc at "https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SES.html#sendTemplatedEmail-property". It was using "sendTemplatedEmail" API we can send email using templates. It was successful. But I could not figure out how to add attachments to it.

In the 4th point of the "sendTemplatedEmail" API doc it says "The total size of the message, including attachments, must be less than 10 MB". How to add the attachment here in this sendTemplatedEmail API?

Also there is a API called "sendRawEmail". But that does not suit my requirement. I need to use templates and also attach documents. Does any one know what to do ??

1 Answers

Take a look at the SendRawEmail example:

/* The following example sends an email with an attachment: */

 var params = {
     Destinations: [], 
     FromArn: "", 
     RawMessage: {
         Data: <Binary String>
     }, 
     ReturnPathArn: "", 
     Source: "", 
     SourceArn: ""
 };
 ses.sendRawEmail(params, function(err, data) {
     if (err) console.log(err, err.stack); // an error occurred
     else     console.log(data);           // successful response
     /*
     data = {
         MessageId: "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000"
     }
     */
 });

Reference: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SES.html

Important: you need to understand the MIME type standards to include your attachment. Take a look at this article.

MIME was defined in 1992 by the Internet Engineering Task Force (IETF). The distinguishing characteristic of a MIME message is the presence of the MIME headers. As long as your mail recipients also have e-mail software that is MIME-compliant (and most e-mail software is), you can swap files containing attachments automatically.

EDIT: This article explains how to include attachment in your body.

MIME completes the illusion of file attachments by allowing the message body to be divided into distinct parts, each with their own headers. The content type multipart/mixed means that the content of the body is divided into blocks separated by "--" + a unique string guaranteed to not be found anywhere else in the message. If you say that your boundary string is "MyBoundaryString", then all occurrences of that string will be treated as a boundary. So it better not be in the message the user typed or it won't be decoded correctly.

Wikipedia also gives an example:

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=frontier

This is a message with multiple parts in MIME format.
--frontier
Content-Type: text/plain

This is the body of the message.
--frontier
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64

PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg
Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==
--frontier--

I assume you are familiar with Base64.

Related