Node.js 当我连接到邮枪时,我一直得到401
我正在学习一门不允许mw使用库和npm的课程,所以这是我精心编制的https请求Node.js 当我连接到邮枪时,我一直得到401,node.js,api,Node.js,Api,我正在学习一门不允许mw使用库和npm的课程,所以这是我精心编制的https请求 helpers.mailgunSendEmail = (toEmail, toName, subject, message, callback) => { // validate parameters const emailRegex = /\S+@\S+\.\S+/; toEmail = typeof(toEmail) === 'string' && em
helpers.mailgunSendEmail = (toEmail, toName, subject, message, callback) => {
// validate parameters
const emailRegex = /\S+@\S+\.\S+/;
toEmail = typeof(toEmail) === 'string' && emailRegex.test(toEmail) ? toEmail.trim() : false;
toName = typeof(toName) === 'string' && toName.trim().length > 2 ? toName.trim() : false;
subject = typeof(subject) === 'string' && subject.trim().length > 2 ? subject.trim() : false;
message = typeof(message) === 'string' && message.trim().length > 2 ? message.trim() : false;
if (toEmail && toName && message) {
// Configure the request payload
const payload = {
'from' : 'Pizza App <weixinpua0227@gmail.com>',
'to' : "weixinpua0227@gmail.com",
'subject' : subject,
'text' : message
};
// Stringify the payload
const stringPayload = querystring.stringify(payload);
// Configure the request details
const requestDetails = {
'protocol' : 'https:',
'hostname' : 'api.mailgun.net',
'method' : 'POST',
'auth' : config.mailgun.mailgunCredential,
'path' : '/v3/' +config.mailgun.sandbox+ '/messages',
'headers' : {
'Content-type' : 'application/x-www-form-urlencoded',
'Content-length' : Buffer.byteLength(stringPayload)
}
};
// Instantiate the request object
let req = https.request(requestDetails, res => {
// Grab the status of the sent request
let status = res.statusCode;
// Callback successfully if the request went through
if (status == 200 || status == 201) {
callback(false);
} else {
callback(status);
}
});
这是存储沙盒域的地方
mailgun:{
sandbox:"sandboxa3a26706f4b74f75b015e91df2f31ebb.mailgun.org",
mailgunCredential:"key-ba7409a41e533fcaecc0454cdf93475b"
}
我硬编码了我的电子邮件(weixinpua0227@gmail.com)
由于mailgun的沙箱只允许将邮件发送给授权用户,因此将其添加到代码中
我的沙盒也在美国地区,所以主机名(api.mailgun.net)应该很好有帮助吗?我在发布这个问题之前已经阅读了这篇文章,并且我已经检查了我的沙盒域所在的区域是美国,我还检查了我发送到的电子邮件是否经过沙盒域验证。在上面的链接中,有一种方法可以将邮件发送给您自己,以检查配置是否正确。我认为你应该先运行它来检查基本配置是否合适。
mailgun:{
sandbox:"sandboxa3a26706f4b74f75b015e91df2f31ebb.mailgun.org",
mailgunCredential:"key-ba7409a41e533fcaecc0454cdf93475b"
}