[英]mailgun not sending email returning undefined data
我正在尝试使用 Angular 4、nodejs、mailgun-js 和 mailgun 在注册流程上实现一个简单的 email 确认。 问题是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
后端显示现在错误,但返回值(正文)未定义且未发送 email。 这是我完整的后端 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 email 都是有效的 email 地址,console.log 中显示的 req.body 是从前端传递的正确数据。
这是前端发送邮件模块:
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)
问题已解决......由 mailgun 凭据中的拼写错误引起。 复制过来的'<''>'括号......线索是禁止错误。
请确保如果您从欧洲发送,还包括这样的host
:
const mg = mailgun({apiKey: MAILGUN_PRIVATE_API_KEY, domain: MAILGUN_DOMAIN_NAME, host:MAILGUN_HOST});
据我所知,欧洲的host
是api.eu.mailgun.net
,而美国的主机是api.mailgun.net
。
Gabriel Arghire 对我来说 100% 正确答案! 最后搜索了整个互联网之后。 无法理解为什么 mailgun 不使用配置发布主机。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.