简体   繁体   English

通过 aws ses 在 node.js 中发送带有附件的邮件

[英]Sending mail via aws ses with attachment in node.js

有没有人有一些示例说明如何使用 aws ses 在node.js发送带有附件的电子邮件?

If you want to avoid suffering, you have to use SES wrapped in Nodemailer.如果你想避免痛苦,你必须使用包裹在 Nodemailer 中的 SES。 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...直接使用 AWS 开发工具包,你不能用ses.sendEmail发送附件,你必须使用ses.sendRawEmail ,这很糟糕,因为你必须用标题和其他东西来形成原始电子邮件,甚至在我做了之后那个,我有一个奇怪的错误,发送了重复的电子邮件......

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" <someone@example.com>',
    to: "someone_else@example.com",
    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: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
  return info; // or something
}

There are plenty of examples for other ways to set attachments: https://nodemailer.com/message/attachments/有很多其他设置附件方式的例子: https : //nodemailer.com/message/attachments/

If you're still in the SES sandbox mode, both the to/from addresses have to be verified.如果您仍处于 SES 沙盒模式,则必须验证两个收件人/发件人地址。 Apply to get out of the sandbox mode on the SES Sending statistics page.在 SES 发送统计信息页面申请退出沙箱模式。

There is a great mailing library called ˇNodemailerˇ it also has support for the Amazon SES.有一个很棒的邮件库,称为 ˇNodemailerˇ,它也支持 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这是一个如何发送带有附件的电子邮件的小例子https://github.com/nodemailer/nodemailer/blob/master/examples/ses.js

But be aware that Amazon has strange errors when your email sending fails.但请注意,当您的电子邮件发送失败时,亚马逊会出现奇怪的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM