gpt4 book ai didi

javascript - 通过 nodemailer 向多个收件人发送电子邮件

转载 作者:IT老高 更新时间:2023-10-28 22:04:00 26 4
gpt4 key购买 nike

我正在尝试向多个收件人发送电子邮件。为此,我创建了一个收件人数组,但是使用我的代码,我只能将邮件发送到数组的最后一个电子邮件 ID 三次。我的代码有什么问题?

var nodemailer = require("nodemailer");

var smtpTransport = nodemailer.createTransport(
"SMTP",{
host: '',
// secureConnection: true, // use SSL
port: 25
});

var maillist = [
'****.sharma3@****.com',
'****.bussa@****.com',
'****.gawri@****.com',
];

var msg = {
from: "******", // sender address
subject: "Hello ✔", // Subject line
text: "Hello This is an auto generated Email for testing from node please ignore it ✔", // plaintext body
cc: "*******"
// html: "<b>Hello world ✔</b>" // html body
}


maillist.forEach(function (to, i , array) {
msg.to = to;

smtpTransport.sendMail(msg, function (err) {
if (err) {
console.log('Sending to ' + to + ' failed: ' + err);
return;
} else {
console.log('Sent to ' + to);
}

if (i === maillist.length - 1) { msg.transport.close(); }
});
});

最佳答案

nodemailer (v2.4.2) docs说:

to - Comma separated list or an array of recipients e-mail addresses that will appear on the To: field

所以你可以这样做:

var maillist = [
'****.sharma3@****.com',
'****.bussa@****.com',
'****.gawri@****.com',
];

var msg = {
from: "******", // sender address
subject: "Hello ✔", // Subject line
text: "Hello This is an auto generated Email for testing from node please ignore it ✔", // plaintext body
cc: "*******",
to: maillist
}

关于javascript - 通过 nodemailer 向多个收件人发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28527561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com