使用 Nodemailer 的 Cloud Functions,电子邮件已发送但从未到达

Cloud Functions using Nodemailer, Email sent but never arrives

我正在使用 Google Cloud Functions 通过 Nodemailer 发送电子邮件,邮件通过 Oauth2 进行身份验证。它似乎没有任何问题,电子邮件已发送并显示在我的 gmail 帐户的已发送电子邮件中,但实际上它们从未到达我的其他电子邮件地址...有人知道为什么吗?

这是我的代码

const mailTransport = nodemailer.createTransport({
  name: "gmail.com",
  host: "smtp.gmail.com",
  port: 465,
  secure: true,
  auth: {
    type: "OAuth2",
    user: gmailEmail,
    pass: gmailPassword,
    clientId: clientid,
    clientSecret: clientsecret,
    refreshToken: clientrefreshtoken,
    accessToken: clientaccesstoken,
    expires: 3599,
  },
});

  exports.submit = functions.https.onRequest((req, res) => {
  const corsMiddleware = cors(corsOptions);
  corsMiddleware(req, res, () => {
    if (req.method !== "POST") {
      return;
    }

    const mailOptions = {
      from: req.body.email,
      replyTo: req.body.email,
      to: "myemail@gmx.at",
      subject: `Absender: ${req.body.email}`,
      html: req.body.message,
    };

    mailTransport.sendMail(mailOptions);
    res.status(200).send({isEmailSend: true});
  });
});

提前致谢

更新! 不知何故,我收到一些电子邮件真的很晚,但不是全部。更像是十分之一,我也在我的邮件收件箱中收到了这个:

The recipient server did not accept our requests to connect. 
Learn more at https://support.google.com/mail/answer/7720 [mx00.emig.gmx.net. 212.227.15.9: 421-gmx.net (mxgmx017) 
Nemesis ESMTP Service not available 421-Service unavailable 421-Reject due to policy restrictions. 
421 For explanation visit https://postmaster.gmx.net/en/error-messages?ip=209.85.222.194&c=sp ] [mx01.emig.gmx.net. 212.227.17.5: 421-gmx.net (mxgmx114) 
Nemesis ESMTP Service not available 421-Service unavailable 421-Reject due to policy restrictions. 
421 For explanation visit https://postmaster.gmx.net/en/error-messages?ip=209.85.222.196&c=sp ]

这可能不是您问题的原因,但您应该等待异步 sendMail() 执行完成后再发回响应。如果您不这样做,您将向 Cloud Functions 平台表明它可以清理实例 运行 您的 Cloud Functions,而无需等待异步任务完成。

  exports.submit = functions.https.onRequest((req, res) => {
  const corsMiddleware = cors(corsOptions);
  corsMiddleware(req, res, () => {
    if (req.method !== "POST") {
      return;
    }

    const mailOptions = {
      from: req.body.email,
      replyTo: req.body.email,
      to: "myemail@gmx.at",
      subject: `Absender: ${req.body.email}`,
      html: req.body.message,
    };

    mailTransport.sendMail(mailOptions)
    .then(() => {
       res.status(200).send({isEmailSend: true});
    })
    
  });
});

所以经过 2 天的尝试,我认为问题与我的代码无关.... 似乎是 gmx 服务器的问题,也许我在任何黑名单上?我不知道,实际上帐户是新的... 没关系从我的域发送到我的私人电子邮件。