我正在尝试通过 Sendgrid 向首次注册的用户发送电子邮件验证。按照此处找到的 v3_mail 示例,我创建了以下实现:
const querystring = require('querystring');
const helper = require('sendgrid').mail;
const https = require('https');
const url = "http://<mywebsite.com>/users/reset/passwordLoggedOut?";
let sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
const emails = {};
function sendMail(mail){
const request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON(),
});
sg.API(request, function(error, response) {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
console.log(error);
});
}
emails.sendActivationEmail = function(user){
let qso = {username: user.username, activationCode: user.activationCode};
let qs = querystring.stringify(qso);
let from = new helper.Email('Thomas.Talhelm@chicagobooth.edu');
let to = new helper.Email(user.username);
let subject = 'Welcome to Psych Study \'16';
let content = new helper.Content('text/html', "<p> Thanks for signing up " +
"for our psych study, please <a href=\"http://<mywebsite.com>/users/validate/account?" +
qs + "\">confirm your email</a></p>");
let mail = new helper.Mail(from, subject, to, content);
sendMail(mail);
console.log('Mail Sent!');
}
module.exports = emails;
每当我注册用户时,最终都会正确调用这些函数,尽管我收到状态代码 202(已收到响应,电子邮件正在排队等待发送),并且电子邮件永远不会被发送。这个问题有点类似于这个问题,但我的活动日志是空白的,即使我已经通过定义明确的环境变量正确输入了 SendGrid API。我尝试重新创建我的 API 密钥,对密钥使用完全权限,但没有从 API 中找到对此行为的解释。