Amazon ses.sendEmail - how to attach pdf file?
Asked Answered
U

3

5

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!");
                }
            });`
Updraft answered 22/9, 2016 at 15:12 Comment(0)
I
8

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);
  });
});
Irkutsk answered 26/10, 2017 at 13:32 Comment(1)
This is old but, how did you parse the incoming PDF file in the aws lambda?Ioneionesco
M
5

Currently, you can only send attachments if you use the raw email API, e.g.:

var ses_mail = "From: 'Your friendly UI5 developer' <" + email + ">\n"
    + "To: " + email + "\n"
    + "Subject: AWS SES Attachment Example\n"
    + "MIME-Version: 1.0\n"
    + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n"
    + "--NextPart\n"
    + "Content-Type: text/html; charset=us-ascii\n\n"
    + "This is the body of the email.\n\n"
    + "--NextPart\n"
    + "Content-Type: text/plain;\n"
    + "Content-Disposition: attachment; filename=\"attachment.txt\"\n\n"
    + "Awesome attachment" + "\n\n"
    + "--NextPart";

var params = {
    RawMessage: { Data: new Buffer(ses_mail) },
    Destinations: [ email ],
    Source: "'Your friendly UI5 developer' <" + email + ">'"
};

var ses = new AWS.SES();

ses.sendRawEmail(params, function(err, data) {
    if(err) {
        oDialog.close();
        MessageToast.show("Email sent successfully!");
    } 
    else {
        oDialog.close();
        MessageToast.show("Email sent successfully!");
    }           
});
Micro answered 23/9, 2016 at 9:5 Comment(6)
Thanks for your reply. I tried this. It is working for .txt formats. When I try to attach pdf, It is corrupted while receiving Email. Is there a solution for this?Updraft
Non-text attachments needs to be encoded. You may want to have a look here for more info and some Javascript code that can facilitate this: github.com/ikr0m/mime-jsMicro
How can I give encoded mail message to ses.sendRawEmail(). Converted my mail params as var mimeTxt = Mime.toMimeTxt(mail);var mimeObj = Mime.toMimeObj(mimeTxt);. Should i have to give "mimeObj " to RawMessage data. If i do that it returns error as RawMessage data accepts only strings,blob,typed arrayUpdraft
@Updraft use Content-Type: application/octet-stream and convert your pdf data to base64Lentz
@Lentz For the attchaments. I am declaring my content type in this way -- For pdf only. Content-Type: application/pdf Content-Transfer-Encoding: base64 My pdf encoded into base64.Anglonorman
Also, for future readers the official AWS docs. (docs.aws.amazon.com/ses/latest/DeveloperGuide/…)Anglonorman
F
0

I've modified jpenninkhof's answer for PDF files, here is how you can send PDF attachment using SES

let attachment = fs.readFileSync("./uploads/" + filename).toString('base64')

            var ses_mail = "From: <[email protected]" + ">\n"
                + "To: " + email + "\n"
                + "Subject: Your email subject\n"
                + "MIME-Version: 1.0\n"
                + "Content-Type: multipart/mixed; boundary=\"NextPart\"\n\n"
                + "--NextPart\n"
                + "Content-Type: text/html; charset=us-ascii\n\n"
                + "This is your email body.\n\n"
                + "--NextPart\n"
                + "Content-Type: application/pdf;\n"
                + "Content-Disposition: attachment; filename= \"filename.pdf\"\n"
                + "Content-Transfer-Encoding: base64\n\n"
                + attachment + "\n\n"
                + "--NextPart";

            var params = {
                RawMessage: { Data: Buffer.from(ses_mail) },
                Destinations: [email],
                Source: "<[email protected]>"
            };

            ses.sendRawEmail(params, function (err, data) {
                if (err) {
                    console.log(err)
                }
                else {
                    console.log("Email sent successfully!");
                }
            });
Frug answered 31/8, 2023 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.