Sending mail via aws ses with attachment in node.js
Asked Answered
P

3

10

Does anyone have some example of how to send email with attachment in node.js with aws ses?

Poco answered 13/4, 2014 at 12:36 Comment(0)
K
1

There is a great mailing library called ˇNodemailerˇ it also has support for the Amazon SES. Here is a small example of how to send e-mail with attachment https://github.com/nodemailer/nodemailer/blob/master/examples/ses.js

But be aware that Amazon has strange errors when your email sending fails.

Kingsley answered 13/4, 2014 at 12:44 Comment(2)
Now Nodemailer has a separate SES transport module.Irritating
Bear in mind nodemailer does not support SESv2. This limits the size of the message (and therefore attachments) to 10MB. - github.com/nodemailer/nodemailer/issues/1165 - docs.aws.amazon.com/ses/latest/dg/quotas.htmlAdsorb
R
27

If you want to avoid suffering, you have to use SES wrapped in Nodemailer. Using the AWS SDK directly, you can't send attachments with ses.sendEmail, you have to use ses.sendRawEmail, which is awful because you have to form the raw E-mail with headers and what-not, and even after I did that, I had a weird error where duplicate E-mails were being sent...

npm install nodemailer

const AWS = require('aws-sdk');
const nodemailer = require("nodemailer");

async function scheduledEmail () {
  let usefulData = 'some,stuff,to,send';
  
  let transporter = nodemailer.createTransport({
    SES: new AWS.SES({ region: 'us-east-1', apiVersion: "2010-12-01" })
  });
  
  let text = 'Attached is a CSV of some stuff.';

  // send mail with defined transport object
  let info = await transporter.sendMail({
    from: '"Some name" <[email protected]>',
    to: "[email protected]",
    subject: "Hello",                // Subject line
    text: text,                      // plaintext version
    html: '<div>' + text + '</div>', // html version
    attachments: [{
        filename: "usefulData.csv",
        content: usefulData
    }]
  });

  console.log("Message sent: %s", info.messageId);
  // Message sent: <[email protected]>
  return info; // or something
}

There are plenty of examples for other ways to set attachments: https://nodemailer.com/message/attachments/

If you're still in the SES sandbox mode, both the to/from addresses have to be verified. Apply to get out of the sandbox mode on the SES Sending statistics page.

Rafferty answered 3/12, 2020 at 21:43 Comment(1)
A-Sharabiani: content can be many things. Refer to the nodemailer docs for info: nodemailer.com/message/attachmentsJezabella
K
1

There is a great mailing library called ˇNodemailerˇ it also has support for the Amazon SES. Here is a small example of how to send e-mail with attachment https://github.com/nodemailer/nodemailer/blob/master/examples/ses.js

But be aware that Amazon has strange errors when your email sending fails.

Kingsley answered 13/4, 2014 at 12:44 Comment(2)
Now Nodemailer has a separate SES transport module.Irritating
Bear in mind nodemailer does not support SESv2. This limits the size of the message (and therefore attachments) to 10MB. - github.com/nodemailer/nodemailer/issues/1165 - docs.aws.amazon.com/ses/latest/dg/quotas.htmlAdsorb
R
1

If you'd like to use the AWS SESv2 without too much suffering but more directly than using NodeMailer you can easily build the mime data using this module

The Content part of the SendEmailCommand would look like:

Content: {
   Raw: {
      Data: Buffer.from(clientMsg.asRaw(), 'utf8')
   }
},

In provided example on module they also demonstrate how to pull the to/from emails from the MIME message to avoid retyping.

Redden answered 17/8, 2023 at 18:17 Comment(1)
Thanks a lot, this library really works! I was trying to solve attachments almost whole day and with this library it was in minute.Evangel

© 2022 - 2024 — McMap. All rights reserved.