制作了一个小脚本,可以从另一个文件中获取电子邮件列表。然后使用forEach来使用来自nodemailer的邮件功能,向每个发送邮件。我的问题是我希望每发送一封电子邮件之间都有一个超时。我已经尝试了几件事,但我完全陷入困境。如果有人可以帮我。这是我的第一个尝试编写代码的项目。
'use strict';
const nodemailer = require('nodemailer');
var fs = require('fs');
//--------------------------------------------------------------------------
// Load Victims
//--------------------------------------------------------------------------
var victims = fs.readFileSync('./victims.txt', 'utf8').split("\r\n");
var letter = fs.readFileSync('./letter.txt', 'utf8');
victims.forEach(function(item, index, array) {
let transporter = nodemailer.createTransport({
pool: true,
maxConnections: 1,
maxMessages: 1,
rateDelta: 3000,
rateLimit: 1,
host: 'mail.asrebaziemast.com',
port: 587,
secure: false, // true for 465, false for other ports
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
},
auth: {
user: "a1@asrebaziemast.com", // generated ethereal user
pass: "D@gician#d21" // generated ethereal password
}
});
let mailOptions = {
from: '"Freddy Mang" <a1@asrebaziemast.com>', // sender address
to: item, // list of receivers
subject: 'Hello ✔', // Subject line
html: letter, // html body
encoding: 'base64'
};
setTimeout(function () {
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId, item);
});
}, 5000);
});
答案 0 :(得分:0)
此处需要rateLimit
/ rateDelta
选项,问题是您正在创建每个电子邮件 的新传输程序实例。
将传输创建移动到forEach
之外,以重复使用同一实例,例如
const transporter = nodemailer.createTransport({
pool: true,
maxConnections: 1,
maxMessages: 1,
rateDelta: 3000,
rateLimit: 1,
host: 'mail.asrebaziemast.com',
port: 587,
secure: false, // true for 465, false for other ports
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
},
auth: {
user: "a1@asrebaziemast.com", // generated ethereal user
pass: "D@gician#d21" // generated ethereal password
}
});
victims.forEach(item => {
transporter.sendMail({
from: '"Freddy Mang" <a1@asrebaziemast.com>', // sender address
to: item, // list of receivers
subject: 'Hello ✔', // Subject line
html: letter, // html body
encoding: 'base64'
}, (err, info) => {
if (err) {
return console.log(err);
}
console.log('Message sent: %s', info.messageId, item);
});
});