I am trying to send an email that contains an attachment (a pdf file) using the amazon SES API.
I am using Symfony2 so I included the AmazonWebServiceBundle in my pr
I managed to create a raw MIME Message in the below manner to send an email with attachment (.pdf file), using Amazon SES sendRawEmail. This is for plain javascript usage; one can surely refine it further add other Content-types too.
Use a library like jsPDF & html2Canvas to create the PDF file & save the contents into a variable & get the base 64 data:
var pdfOutput = pdf.output();
var myBase64Data = btoa(pdfOutput);
Use the below code to create the MIME Message. Note, the sequence is important else the email will end up as a text email exposing all the base 64 data :
var fileName = "Attachment.pdf";
var rawMailBody = "From: testmail@gmail.com\nTo: testmail@gmail.com\n";
rawMailBody = rawMailBody + "Subject: Test Subject\n";
rawMailBody = rawMailBody + "MIME-Version: 1.0\n";
rawMailBody = rawMailBody + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n";
rawMailBody = rawMailBody + "--NextPart\n";
rawMailBody = rawMailBody + "Content-Type: application/octet-stream\n";
rawMailBody = rawMailBody + "Content-Transfer-Encoding: base64\n";
rawMailBody = rawMailBody + "Content-Disposition: attachment; filename=\"" + fileName + "\"\n\n";
rawMailBody = rawMailBody + "Content-ID random2384928347923784letters\n";
rawMailBody = rawMailBody + myBase64Data+"\n\n";
rawMailBody = rawMailBody + "--NextPart\n";
Invoke sendRawEmail:
var params = {
RawMessage: {
Data: rawMailBody
},
Destinations: [],
Source: 'testmail@gmail.com'
};
ses.sendRawEmail(params, function(err, data) {
if (err) alert("Error: "+err); // an error occurred
else {
alert("Success: "+data); // successful response
}
});