Mohammad Saad
Mohammad Saad

Reputation: 127

Cloud function have to be called twice before sending mail with Nodemailer

When I'm trying to call my cloud function (which sends a mail with nodemailer) I have to call it once before it actually "activates" and afterwards it works normally until some time have passed and the same process is necessary.

On the first call I do not receive logs.

exports.sendEmail = functions.https.onRequest((req, res) => {
  console.log(req.body);

  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      return console.log(error);
    }
    console.log("Message sent: %s", info.messageId);
    console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));

    return res.sendStatus(200);
  });
});

Upvotes: 0

Views: 196

Answers (1)

andresmijares
andresmijares

Reputation: 3744

A few ways to promisify your response.

const sendMail = function(mailOptions, transporter) {
  return new Promise(function(resolve, reject) {
    transporter.sendMail(mailOptions, function(error, info) {
      if (error) {
        reject(error);
      } else {
        resolve(info);
      }
    });
  });
};

Another way is to do something like

return transporter.sendMail(mailOptions).then(function(response) {
  return res.sendStatus(200);
}).catch(function(error) {
  console.info(error);
})

I would put my coins on that.

Upvotes: 1

Related Questions