I want to send PDF file in attachment using sendRawEmail(Node: aws-sdk) function, I have tried lots of ways, email sends successfully but PDF goes plain. Please correct my code and help to solve it.
Code is here:
try {
data = fs.readFileSync('files/demo-invoice-new.pdf', 'utf8');
console.log(data.toString());
var ses_mail = "From: 'AWS SES Attchament Configuration' <" + SOURCE_EMAIL + ">\n";
ses_mail = ses_mail + "To: " + toEmail + "\n";
ses_mail = ses_mail + "Subject: AWS SES Attachment Example\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 + "This is the body of the email.\n\n";
ses_mail = ses_mail + "--NextPart\n";
ses_mail = ses_mail + "Content-Type: application/octet;\n";
ses_mail = ses_mail + "Content-Disposition: attachment; filename=\"demo-invoice-new.pdf\"\n\n";
ses_mail = ses_mail + data.toString('utf8') + "\n\n";
ses_mail = ses_mail + "--NextPart--";
var params = {
RawMessage: { Data: new Buffer(ses_mail) },
Destinations: [toEmail],
Source: "'AWS SES Attchament Configuration' <" + SOURCE_EMAIL + ">'"
};
console.log(params);
var sendPromise = new AWS.SES(AWS_SES_CONFIG).sendRawEmail(params).promise();
return sendPromise.then(
data => {
console.log(data);
return data;
}).catch(
err => {
console.error(err.message);
throw err;
});
} catch (e) {
console.log('Error:', e.stack);
}