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!");
                }
            });`
Hari
  • 277
  • 3
  • 16
  • 28

3 Answers3

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);
  });
});
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
arseneoaa
  • 286
  • 5
  • 5
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!");
    }           
});
jpenninkhof
  • 1,920
  • 1
  • 11
  • 12
  • 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? – Hari Sep 28 '16 at 09:49
  • 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: https://github.com/ikr0m/mime-js – jpenninkhof Sep 29 '16 at 16:09
  • 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 array – Hari Sep 30 '16 at 14:44
  • @Hari use Content-Type: application/octet-stream and convert your pdf data to base64 – samarth Aug 13 '19 at 12:36
  • @samarth 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. – Franco Gil Jun 11 '20 at 16:00
  • Also, for future readers the official AWS docs. (https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-raw.html) – Franco Gil Jun 11 '20 at 16:02
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: <source@example.com" + ">\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: "<source@example.com>"
            };

            ses.sendRawEmail(params, function (err, data) {
                if (err) {
                    console.log(err)
                }
                else {
                    console.log("Email sent successfully!");
                }
            });
Uzair Saeed
  • 61
  • 1
  • 5