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 c
Hi there for anybody who stumbles upon this problem I managed to solve it using nodemailer and SESV2, I had base64 encoded data so your script might be a little different from mine but the snippet below should give you an idea... Here's my solution hope it helps someone:
const MailComposer = require("nodemailer/lib/mail-composer");
const AWS = require("aws-sdk");
const generateRawMailData = (message) => {
let mailOptions = {
from: message.fromEmail,
to: message.to,
subject: message.subject,
text: message.bodyTxt,
html: message.bodyHtml,
attachments: message.attachments.map(a => ({ filename: a.name, content: a.data, encoding: 'base64' }))
};
return new MailComposer(mailOptions).compile().build();
};
const exampleSendEmail = async () => {
var message = {
fromEmail: "sender@server.com",
to: "receiver@sender.com",
subject: "Message title",
bodyTxt: "Plaintext version of the message",
bodyHtml: "HTML version of the message
",
attachments: [{
name: 'hello.txt',
data: 'aGVsbG8gd29ybGQ='
}]
};
let ses = new AWS.SESV2(),
params = {
Content: { Raw: { Data: await generateRawMailData(message) } },
Destination: {
ToAddresses: message.to,
BccAddresses: message.bcc,
},
FromEmailAddress: message.fromEmail,
ReplyToAddresses: message.replyTo,
};
return ses.sendEmail(params).promise();
}