我正在尝试通过Sendgrid向已注册第一个用户的用户发送电子邮件验证。在找到here的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(收到回复,电子邮件排队等待发送),但电子邮件永远不会被发送。这个问题有点类似于this one,但是我的活动日志是空白的,即使我已经通过一个定义良好的环境变量正确地输入了SendGrid API。我尝试重新创建我的API密钥,对密钥使用了完全权限,但没有从API中找到对此行为的解释。