【问题标题】:How can I send multiple emails (around 100) with different email body using amazon ses in NodeJS?如何在 NodeJS 中使用 amazon ses 发送具有不同电子邮件正文的多封电子邮件(大约 100 封)?
【发布时间】:2021-12-02 15:42:30
【问题描述】:

我正在尝试使用电子邮件正文向多个用户发送电子邮件

dear {{username}},
/.
....
Your email is {{email}}
...
.
/

我该怎么做?

【问题讨论】:

  • 您的{{email}} 将从数据文件中选择电子邮件地址。如果您没有数据文件并且只是循环记录记录,那么它将需要一个变量来替换将发送出去的 HTML 中的文本。

标签: amazon-ses email-templates


【解决方案1】:

您可以使用 SES 批量模板电子邮件。

  1. 为您的电子邮件创建模板。

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

const ses = new AWS.SES({
    accessKeyId: <<YOUR_ACCESS_KEY>>,
    secretAccessKey: <<YOUR_ACCESS_KEY>>,
    region: <<YOUR_ACCESS_KEY>>
  }); 

const params = {
  Template: {
    TemplateName: "MyTemplate",
    SubjectPart: "Test mail for {{username}}!",
    HtmlPart: "<p>Dear {{username}}</p>, <p>Your email is {{email}}.</p>"
  }
}

ses.createTemplate(params, (err, data) =>  {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});
  1. 完成后,您将在 SES 控制台的电子邮件模板下看到 MyTemplate。我们不再需要模板创建部分代码。

  2. 现在我们可以使用以下方式发送电子邮件了。

const users = [{username:"max", email: "max@m.com"},{username: "mosh", email:"mosh@h.com"}] // sample array of users

let destinations = []

for (const user of users) {
    destinations.push({
       Destination: {
         ToAddresses: [user.email]
       },
        ReplacementTemplateData: JSON.stringify({
          username: user.username,  // This will provide the value for username in template
          email: user.email // This will provide the value for email in template
        })
     });
}           

const params = {
   Source: "sender@xyz.com", // sender email
   Template: "MyTemplate", // Template name we have created
   Destinations: destinations, 
   DefaultTemplateData: JSON.stringify({
      username: '', // default value for username
      email: '' // default value for email
   })
}

ses.sendBulkTemplatedEmail(params, (err, data) =>  {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

在运行此之前,请确保您已为 IAM 用户授予 ses:createTemplate 和 ses:sendBulkTemplatedEmail 权限。

欲了解更多信息,请参阅here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多