繁体   English   中英

nodemailer 发送带有特定收件人的特定信息的邮件

[英]nodemailer send mail with specific information for specific receiver

我正在使用 nodemailer 向多个用户发送邮件,但我遇到了这个问题,我有一个这样的 json 文件:

[
  {
    "email": "dotam1236@gmail.com",
    "content": "this email send for dotam1236@gmail.com"
  },
  {
    "email": "sale.shopeeholic@gmail.com",
    "content": "this email send for sale.shopeeholic@gmail.com"
  },
  {
    "email": "ducminhtuhp@gmail.com",
    "content": "this email send for ducminhtuhp@gmail.com"
  },
  {
    "email": "xrodell3@vimeo.com",
    "content": "this email send for xrodell3@vimeo.com"
  },
  {
    "email": "pbeadle4@cnet.com",
    "content": "this email send for pbeadle4@cnet.com"
  }
]

如您所见,每封电子邮件都有一组接收器和内容,这是我的 nodemailer 代码:

  var transporter = nodemailer.createTransport({
    service: "Gmail",
    auth: {
      user: "your@gmail.com", // please modify it into your gmail
      pass: "yourpassword!" // please modify it into your password of gmail and remove it when you success
    }
  });

  var mailOptions = {
    from: "sale.shopeeholic@gmail.com",
    to: "dotam1236@gmail.com, sale.shopeeholic@gmail.com",
    cc: "sale.shopeeholic@gmail.com",
    subject: "Test",
    text: "nodemailer send multiple receivers successfully"
  };

  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      console.log(error);
      return res.status(400).json({ message: "error in sending mail", error });
    } else {
      return res.status(200).json({ message: "mail sent" });
    }
  });

我如何为上面json中的每个电子邮件地址发送带有具体内容的邮件,非常感谢您的帮助,如果您不介意,请在此处的codeandbox链接中修改它(服务器是ExpressJs但没关系,它不会有太大影响)

在该链接中,您应该使用自己的 gmail 和电子邮件密码进行修改,但我向上帝发誓,我永远不会让它侵入您的个人信息,您可以使用您的个人信息并在成功后将其删除。 再次非常感谢你。 希望你有美好的一天

我们可以在一个数组中一次发送多封电子邮件。

var transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
  user: "your@gmail.com", // please modify it into your gmail
  pass: "yourpassword!" // please modify it into your password of gmail and remove it when you success
}


});



var mailOptions = {
from: "sale.shopeeholic@gmail.com",
to: ["dotam1236@gmail.com, sale.shopeeholic@gmail.com"],
cc: "sale.shopeeholic@gmail.com",
subject: "Test",
text: "nodemailer send multiple receivers successfully"


};



transporter.sendMail(mailOptions, (error, info) => {
if (error) {
  console.log(error);
  return res.status(400).json({ message: "error in sending mail", error });
} else {
  return res.status(200).json({ message: "mail sent" });
}
  });

暂无
暂无

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

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