Reputation: 327
I am using NodeJs to send email (through aws ses)with attachment pdf. currently I am trying add the html content inside the pdf attachment. And when I received the attached pdf, I am unable to open the pdf file. I am getting error that the file has been damaged. For your information, I have added the code here. Please let me know if I am missing anything in this config
var message = '<html><body ><div style="margin: auto; width: 96%; border: 6px solid black; vertical-align:middle; text-align:center;height:auto"><img style="width: 204px; height: 128px;" src="image.png" /></div></body></html>';
var ses_mail = "From: 'Email' <" + email + ">\n";
ses_mail = ses_mail + "To: " + email + "\n";
ses_mail = ses_mail + "Subject: Subject Email\n";
ses_mail = ses_mail + "MIME-Version: 1.0\n";
ses_mail = ses_mail + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n";
ses_mail = ses_mail + "--NextPart\n";
ses_mail = ses_mail + "Content-Type: text/html; charset=us-ascii\n\n";
ses_mail = ses_mail + message+"\n";
ses_mail = ses_mail + "--NextPart\n";
ses_mail = ses_mail + "Content-Type: application/pdf;\n";
ses_mail = ses_mail + "Content-Disposition: attachment; filename=\"attachment.pdf\"\n\n";
ses_mail = ses_mail + "Content-Transfer-Encoding: utf-8\n\n"
ses_mail = ses_mail + message + "\n";
ses_mail = ses_mail + "--NextPart--";
var params = {
RawMessage: { Data: new Buffer(ses_mail)},
Destinations: [ email ],
Source: "'AWS Tutorial Series' <" + email + ">'"
};
ses.sendRawEmail(params, function(err, data) {
if(err) {
console.log('failed');
}
else {
console.log('success');
}
});
});
Upvotes: 0
Views: 2970
Reputation: 51
The content of the PDF you are trying to add is the html message, which of course is not a valid content for a PDF. What you can do is to read the content of a PDF file as Buffer and then encode it as base64, then change the transfer encoding to base64
pdfContent = pdfBuffer.toString('base64');
ses_mail = ses_mail + "Content-Disposition: attachment; filename=\"attachment.pdf\"\n\n";
ses_mail = ses_mail + "Content-Transfer-Encoding: base64\n\n"
ses_mail = ses_mail + pdfContent + "\n";
Upvotes: 2