我正在尝试使用Angular 4,nodejs,mailgun-js和mailgun在注册流程上实现简单的电子邮件确认。问题是邮件发送:
mailgun.messages().send(data, function(error, body) {
console.log(body);
});
在前端超时时出现以下错误:
POST http://localhost:3000/users/sendMail net::ERR_EMPTY_RESPONSE
core.js:1448 ERROR
HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}
error
:
ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: "error", …}
headers
:
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message
:
"Http failure response for (unknown url): 0 Unknown Error"
name
:
"HttpErrorResponse"
ok
:
false
status
:
0
statusText
:
"Unknown Error"
url
:
null
__proto__
:
HttpResponseBase
后端显示现在出错但返回值(正文)未定义且未发送电子邮件。这是我完整的后端sendMail模块:
const api_key = '<key-f40360259dea7c667ca06d4aee956421>';
const DOMAIN = '<sandbox836cdf3cfe9c467b916fe5bb0d73e7e7.mailgun.org>';
const mailgun = require('mailgun-js')({ apiKey: api_key, domain: DOMAIN });
// ---------------- sendMail variables -----
router.post('/sendMail', (req, res, next) => {
console.log("Email message data: " + JSON.stringify(req.body));
const data = {
from: 'xxxxdev@gmail.com',
to: [req.body.email, 'xxxxxxx@gmail.com'],
name: req.body.firstName,
code: req.body.workshopCode,
subject: req.body.workshopTitle,
text: req.body.messageBody
};
mailgun.messages().send(data, function(error, body) {
console.log(body);
});
});
From和To电子邮件都是有效的电子邮件地址,console.log中显示的req.body是从前端传递的正确数据。
这是前端sendmail模块:
sendEmailConfirmation(message) {
const headers = new HttpHeaders();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post('http://localhost:3000/users/sendMail', message).subscribe((data) => {
console.log('Mail return data: ' + data); >>>THIS LINE IS NEVER EXECUTED<<<<<<
},
);
}
我在这里缺少什么?.....
我注意到没有res.send所以我添加了以下内容:
if (error) throw error;
else {
res.send('Email sent');
}
现在我在后端遇到了一个禁止的错误:
(node:10780) UnhandledPromiseRejectionWarning: Error: Forbidden
at IncomingMessage.res.on (C:\connect2Server\node_modules\mailgun-js\lib\request.js:301:17)
at IncomingMessage.emit (events.js:165:20)
at endReadableNT (_stream_readable.js:1101:12)
at process._tickCallback (internal/process/next_tick.js:152:19)
(node:10780) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a ca
tch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
答案 0 :(得分:0)
问题解决了.....由邮件枪凭证中的拼写错误引起的。 '&lt;' '&GT;'复制过的括号.....线索是Forbidden错误。
答案 1 :(得分:0)
请确保如果您从欧洲发送,还包括 host
,如下所示:
const mg = mailgun({apiKey: MAILGUN_PRIVATE_API_KEY, domain: MAILGUN_DOMAIN_NAME, host:MAILGUN_HOST});
据我所知,对于欧洲来说,host
是 api.eu.mailgun.net
,对于美国来说是 api.mailgun.net
。