这是我的代码:
userDetails = [{email:'email',id:'id'}]
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey('key');
for (const i in usersDetails) {
const user = usersDetails[i];
const msg = {
from: `ZdajTo<noreply@zdajto.com>`,
to: user.email,
subject: `Dostepne sa nowe zadania z kategorii ${work.category}!`,
html: `<p>Hej! Sprawdz aplikacje ZdajTo! Dostepne sa nowe zadania z kategorii ${work.category}! Aby zrezygnowac z otrzymywania emaili kliknij w <a href="https://Link/${user.id}" target="_blank">ten link</a></p>`,
};
calls.push(sgMail.send(msg).then(() => console.log(`Email sent to ${msg.to}`)).catch(e => console.log(e)));
}
return Promise.all(calls).then(() => console.log('Emails sent')).catch(err => console.log(8, err));
userDetails是一组带有id的电子邮件。
我之前在nodemailer中使用了几乎相同的代码,而且效果很好。这是nodemailer版本:
for (const i in usersDetails) {
const user = usersDetails[i];
calls.push(mailTransport.sendMail({
from: `ZdajTo <noreply@zdajto.com>`,
to: user.email,
subject: `Dostepne sa nowe zadania z kategorii ${work.category}!`,
html: `<p>Hej! Sprawdz aplikacje ZdajTo! Dostepne sa nowe zadania z kategorii ${work.category}! Aby zrezygnowac z otrzymywania emaili kliknij w <a href="https://Link/${user.id}" target="_blank">ten link</a></p>`,
}).catch(err => console.log(7, err, user.email)));
}
奇怪的是,它每次都说Emails sent
和Email sent to...
。没有抛出任何错误,但同时SendGrid表示没有请求。知道我错过了什么吗?
我的假设是您得到了一个可接受的响应(202(,它触发了"发送消息"部分。但是,它不能保证您的消息会被发送,我只是将其排队处理。在这种情况下,我会联系Sendgrid支持:Sendgrid返回202,但没有';t发送电子邮件