Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon ses.sendEmail - how to attach pdf file?

I am trying to attach pdf in email using Amazon ses.sendEmail. But i don't know the param key to do it. Without attachment it is working perfectly. Here is what I have tried.

` var ses = new AWS.SES()

            var params = {
                Destination: { 
                    ToAddresses: [
                        'xxx',
                    ]

                },
                Message: { 
                    Body: { 
                        Html: {
                            Data: msg,
                            Charset: 'UTF-8'
                        }

                    },
                    Subject: { /* required */
                        Data: 'Test Mail',
                        Charset: 'UTF-8'
                    }
                },
                Attachment:{

                },
                Source: 'yyy'
            };
            ses.sendEmail(params, function(err, data) {
                if (err) {// an error occurred}
                    oDialog.close();
                    MessageToast.show("Email not sent. Some problem occurred!");
                }
                else {
                    oDialog.close();
                    MessageToast.show("Email sent successfully!");
                }
            });`
like image 265
Hari Avatar asked Dec 24 '22 01:12

Hari


1 Answers

For anyone else that want to add attachments to an SES email, here is what I did for a lambda in NodeJS: use Nodemailer with the SES transport.

npm install --save nodemailer

and in the code:

// create Nodemailer SES transporter
const transporter = nodemailer.createTransport({
  SES: new AWS.SES({
    apiVersion: '2010-12-01',
    region: "eu-west-1", // SES is not available in eu-central-1
  })
});

const emailTransportAttachments = [];
if (attachments && attachments.length !== 0) {
  emailTransportAttachments = attachments.map(attachment => ({
    filename: attachment.fileName,
    content: attachment.data,
    contentType: attachment.contentType,
  }));
}
const emailParams = {
  from,
  to,
  bcc,
  subject,
  html,
  attachments: emailTransportAttachments,
};

return new Promise((resolve, reject) => {
  transporter.sendMail(emailParams, (error, info) => {
    if (error) {
      console.error(error);
      return reject(error);
    }
    console.log('transporter.sendMail result', info);
    resolve(info);
  });
});
like image 82
arseneoaa Avatar answered Apr 08 '23 10:04

arseneoaa