node.jsamazon-web-servicesemail-attachmentsamazon-sesmime

How do you attach multiple files as attachments in AWS SES with MIME texts using NodeJS?


I am using AWS SES to send emails with attachments, since the sendEmail function is not capable of sending attachments as of now, I'm using the sendRawEmail function in SES. To send the raw email I have converted the email into a MIME text as follows. It has two attachments (images converted to base64) a body and some headers.

From: from@test.com
To: to@test.com
Cc: 
Bcc: 
Subject: Test from Lambda
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="----=_Part2904462804807624"


------=_Part2904462804807624
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit


Hello world


------=_Part2904462804807624
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit


<h1>Hello</h1><p>world</p>


------=_Part2904462804807624
Content-Type: image/jpeg; name="sea-surface-turquoise-blue-color-background-some-reflections-calm-crystal-clear-water-small-ripples-sea-surface-turquoise-221064440.jpg"
Content-Description: sea-surface-turquoise-blue-color-background-some-reflections-calm-crystal-clear-water-small-ripples-sea-surface-turquoise-221064440.jpg
Content-Disposition: attachment;filename="sea-surface-turquoise-blue-color-background-some-reflections-calm-crystal-clear-water-small-ripples-sea-surface-turquoise-221064440.jpg";
Content-Transfer-Encoding: base64


/9j/4AAQSkZJRgABAQAAAQABAAD/4gIcSUNDX1BST0ZJTEUAAQEAAAIMbGNtcwIQAABtbnRyUkdCIFhZWiAH3AABABkAAwApADlhY3NwQVBQTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLWxjbXMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAApkZXNjAAAA/
.....................................
.....................................
.....................................
MOO3FwVjiwfsg1fcci0NRnULhYioZ2KOJRlg33mWBSNrslR+COggrOdzIshEqj4ghEN3WU6jhDlg8zDHGYoRsMNKcauLzWjjzBY26eC5ZFlpeI0fITV8EdMstRZcj4mTDEpYYeTuUa1+yBUdHBcs8rdahAOUiCgJLUZEn/9k=


------=_Part2904462804807624--
Content-Type: image/jpeg; name="image.desktop.size-xl.sme-greenoption-xl1280x720.jpeg"
Content-Description: image.desktop.size-xl.sme-greenoption-xl1280x720.jpeg
Content-Disposition: attachment;filename="image.desktop.size-xl.sme-greenoption-xl1280x720.jpeg";
Content-Transfer-Encoding: base64


/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCALQBQADASIAAhEBAxEB/
.....................................
.....................................
.....................................
ojzPGee6OP61rQa14b06zmutOtLmw1KJSsSTpvILcZV+gx7it/wJaNeeEbqGWK2uIgSttHKoIRwD8p7gZx/Sli023sNae2j0mzS9njKkGcm3kTA3ADdlST6qc150q1HmdNrbs/0Dof/2Q==


------=_Part2904462804807624--
------=_Part2904462804807624--

I have removed a part of the base64 encoding as it's too long. This MIME text works fine with SES but only attaches the first file. The second file is not getting attached to the email. I have tested the second file separately and it works fine. What could be wrong with the MIME text?

Screenshot of the email with only the first attachment


Solution

  • Found the reason for this issue. In MIME texts the final boundary should end with double dashes --. But in my case, I have used double dashes to separate the two attachments which has caused to end the MIME text multipart boundary without the final attachment. The following is the corrected MIME text to add multiple attachments.

    From: from@test.com
    To: to@test.com
    Cc: 
    Bcc: 
    Subject: Test from Lambda
    MIME-Version: 1.0
    Content-Type: multipart/alternative; boundary="----=_Part2904462804807624"
    
    
    ------=_Part2904462804807624
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 7bit
    
    
    Hello world
    
    
    ------=_Part2904462804807624
    Content-Type: text/html; charset=UTF-8
    Content-Transfer-Encoding: 7bit
    
    
    <h1>Hello</h1><p>world</p>
    
    
    ------=_Part2904462804807624
    Content-Type: image/jpeg; name="sea-surface-turquoise-blue-color-background-some-reflections-calm-crystal-clear-water-small-ripples-sea-surface-turquoise-221064440.jpg"
    Content-Description: sea-surface-turquoise-blue-color-background-some-reflections-calm-crystal-clear-water-small-ripples-sea-surface-turquoise-221064440.jpg
    Content-Disposition: attachment;filename="sea-surface-turquoise-blue-color-background-some-reflections-calm-crystal-clear-water-small-ripples-sea-surface-turquoise-221064440.jpg";
    Content-Transfer-Encoding: base64
    
    
    /9j/4AAQSkZJRgABAQAAAQABAAD/4gIcSUNDX1BST0ZJTEUAAQEAAAIMbGNtcwIQAABtbnRyUkdCIFhZWiAH3AABABkAAwApADlhY3NwQVBQTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLWxjbXMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAApkZXNjAAAA/
    .....................................
    .....................................
    .....................................
    MOO3FwVjiwfsg1fcci0NRnULhYioZ2KOJRlg33mWBSNrslR+COggrOdzIshEqj4ghEN3WU6jhDlg8zDHGYoRsMNKcauLzWjjzBY26eC5ZFlpeI0fITV8EdMstRZcj4mTDEpYYeTuUa1+yBUdHBcs8rdahAOUiCgJLUZEn/9k=
    
    
    ------=_Part2904462804807624
    Content-Type: image/jpeg; name="image.desktop.size-xl.sme-greenoption-xl1280x720.jpeg"
    Content-Description: image.desktop.size-xl.sme-greenoption-xl1280x720.jpeg
    Content-Disposition: attachment;filename="image.desktop.size-xl.sme-greenoption-xl1280x720.jpeg";
    Content-Transfer-Encoding: base64
    
    
    /9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCALQBQADASIAAhEBAxEB/
    .....................................
    .....................................
    .....................................
    ojzPGee6OP61rQa14b06zmutOtLmw1KJSsSTpvILcZV+gx7it/wJaNeeEbqGWK2uIgSttHKoIRwD8p7gZx/Sli023sNae2j0mzS9njKkGcm3kTA3ADdlST6qc150q1HmdNrbs/0Dof/2Q==
    
    
    ------=_Part2904462804807624
    ------=_Part2904462804807624--