【问题标题】:mailgun not sending email returning undefined datamailgun不发送返回未定义数据的电子邮件
【发布时间】:2018-09-17 13:54:42
【问题描述】:

我正在尝试使用 Angular 4、nodejs、mailgun-js 和 mailgun 对注册流程实施简单的电子邮件确认。问题是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 是从前端传递的正确数据。

这是前端发送邮件模块:

 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 API 调用的回调中记录错误?您收到ERR_EMPTY_RESPONSE 的原因是因为您在发送邮件后从未结束请求,例如res.sendStatus(200) 表示成功或res.sendStatus(500) 表示错误。
  • @Svenskunganka 我确实注意到了这一点并以(参见编辑)res.send('......')结束。我得到以下信息:...(节点:10780)UnhandledPromiseRejectionWarning:错误:禁止

标签: node.js angular http mailgun


【解决方案1】:

问题已解决......由 mailgun 凭据中的拼写错误引起。复制过来的''括号......线索是禁止错误。

【讨论】:

    【解决方案2】:

    如果您从欧洲发送,请确保还包括host,如下所示:

    const mg = mailgun({apiKey: MAILGUN_PRIVATE_API_KEY, domain: MAILGUN_DOMAIN_NAME, host:MAILGUN_HOST});
    

    据我所知,对于欧洲,hostapi.eu.mailgun.net,对于美国,它是 api.mailgun.net

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 2022-10-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 2018-12-05
      • 1970-01-01
      相关资源
      最近更新 更多