Я хотел бы отправлять электронные письма каждые 200 мс, но кажется, что они не работают с обещаниями:
Моя функция:
SendMail.send = function(from, to, subject, body, name, logo) {
var transporter, html,
contactparam = _.find(options.blocks, { name: 'contactparam' }).params,
smtpUsername = _.find(contactparam, { name: 'smtp_username' }),
smtpPassword = _.find(contactparam, { name: 'smtp_password' }),
smtpHost = _.find(contactparam, { name: 'smtp_host' }),
smtpPort = _.find(contactparam, { name: 'smtp_port' }),
compagny = _.find(contactparam, { name: 'compagny' }),
firstname = _.find(contactparam, { name: 'firstname' }),
facebookUrl = _.find(_.find(options.blocks, { name: 'socialparam' }).params, { name: 'facebook' }).value,
twitterUrl = _.find(_.find(options.blocks, { name: 'socialparam' }).params, { name: 'twitter' }).value,
linkedinUrl = _.find(_.find(options.blocks, { name: 'socialparam' }).params, { name: 'linkedin' }).value,
secondaryColor = _.find(_.find(options.blocks, { name: 'themeparam' }).params, { name: 'secondary_color' }).value,
loadedLogo = logo || _.find(_.find(options.blocks, { name: 'logoparam' }).params, { name: 'logo' }).value,
websiteUrl = _.find(_.find(options.blocks, { name: 'mainparam' }).params, { name: 'url' }).value,
templateFile = fs.readFileSync('./app/views/0-templates/' + options.template + '/mail.ejs', 'utf-8'),
loadedName = name || (compagny && compagny.value) || (firstname && name && firstname.value + ' ' + name.value),
ejsTemplate = ejs.compile(templateFile);
html = ejsTemplate({
logo: loadedLogo || '/images/LOGO.png',
websiteUrl: websiteUrl,
body: body,
title: subject,
facebookUrl: facebookUrl,
twitterUrl: twitterUrl,
linkedinUrl: linkedinUrl,
secondaryColor: secondaryColor || '#1c1c1c'
});
if ((smtpUsername && smtpUsername.value !== '') && (smtpPassword && smtpPassword.value !== '') && (smtpHost && smtpHost.value !== '') && (smtpPort && smtpPort.value !== '')) {
transporter = nodemailer.createTransport({
host: smtpHost.value,
pool: true,
port: parseInt(smtpPort.value, 10),
auth: {
user: smtpUsername.value,
pass: smtpPassword.value
}
});
} else {
transporter = nodemailer.createTransport({
sendmail: true,
newline: 'unix',
path: '/usr/sbin/sendmail'
});
}
return transporter.sendMail({
from: loadedName + ' <' + from + '>',
replyTo: from,
to: to,
subject: subject,
html: html
});
};
SendMail.arraySend = function(array) {
var promises = [];
promises = array.map(function (emailObj, index) {
return new Promise(function () {
setTimeout(function () {
return sendMailHelper.send(emailObj.from, emailObj.to, emailObj.title, emailObj.htmlMessage);
}, 200 * index);
});
});
return promises;
};
Использование моего функция:
Promise.all(sendMailHelper.arraySend(emailsArray)).then( function() {
request.flash('success', 'Emails sent !');
response.redirect('/');
});
emailsArray:
emailsArray.push({
from: from,
to: registration.cfc_user.email,
title: 'Finale : ' + newTournament.name,
htmlMessage: htmlMessage
});
Проблема: электронные письма не отправляются.