Search code examples
node.jsoauth-2.0google-cloud-functionsnodemailer

Cloud Functions using Nodemailer, Email sent but never arrives


I am using Google Cloud Functions to send E-Mail via Nodemailer, the mails are authenticated via Oauth2. It seems to work without any problems, the emails get sent and are also shown in the sent e-mails of my gmail account, but they actually never arrive at my other email address... Does someone know why?

This is my Code

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: "[email protected]",
      subject: `Absender: ${req.body.email}`,
      html: req.body.message,
    };

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

Thanks in Advance

Update! Somehow i get some e-mails really late, but not all of them. More like 1 out of 10 and i also got this in my Mail inbox:

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 ]

Solution

  • So after 2 days of trying i think the problem has nothing to do with my code.... Seems to be a problem with gmx servers, maybe i am on any blacklist? i dont know, account is new actually... Nevermind sending it to my private email from my domain worked.