Ad

React + NodeJS - Nodemailer Google Cloud Functions

I am trying to make a reactJS app, and would like to when people ended up filling the form to send some emails.

for that, I have firstly created a sendGrid function but did not like so much the delay and the email templates (and the fact that is more than 100 mails per day.

then I read that I could use Nodemailer + (or without) express + firebase and google cloud functions. I wrote this function:

exports.mailSun = functions.https.onRequest((req, res) => {
  const { name, dest } = req.query;

  var transporter = nodemailer.createTransport({
    service: 'Gmail',
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        user: '[email protected]',
        pass: 'xxx'
  }
  });
  const mailOptions = {
    from: '[email protected]', // sender address
    to: '[email protected]', // list of receivers
    subject: 'Elavtal', // Subject line
    html: '<p>Grattis för ditt nya avtal. Elbolag kommer att kontakta dig inom kort.</p>'// plain text body
  };

return transporter.sendMail(mailOptions, function (err, info) {
  if(err)
    console.log(err)
  else
    console.log(info);
});
}); 

then I tried

exports.sendMail= functions.https.onRequest((req, res) => {

  cors(req, res, () => {

      // getting dest email by query string
      const { name, dest } = req.query;
      var transporter = nodemailer.createTransport({
        service: 'Gmail',
          host: "smtp.gmail.com",
          port: 465,
          secure: true,
          auth: {
              user: '[email protected]',
              pass: 'xxx'
        }
        });
      const mailOptions = {
          from: 'Sunny <[email protected]>', // Something like: Jane Doe <[email protected]>
          to: '[email protected]',
          subject: 'nytt Avtal', // email subject
          text: `<p Ni har fått ett nytt avtal. Logga in på Sunny för att se kunddetaljer.</p> ` // email content in HTML
      };

      // returning result
      return transporter.sendMail(mailOptions, (erro, info) => {
          if(erro){
              return res.send(erro.toString());
          }
          return res.send('Sent');
      });
  });    
});

can someone help me with what is wrong?

Ad

Answer

Instead of using the callback version of the sendMail() method, use the Promise version, as follows:

exports.mailSun = functions.https.onRequest((req, res) => {
    const { name, dest } = req.query;

    var transporter = nodemailer.createTransport({
        service: 'Gmail',
        host: 'smtp.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: '[email protected]',
            pass: 'xxx'
        }
    });
    const mailOptions = {
        from: '[email protected]', // sender address
        to: '[email protected]', // list of receivers
        subject: 'Elavtal', // Subject line
        html: '<p>Grattis för ditt nya avtal. Elbolag kommer att kontakta dig inom kort.</p>'// plain text body
    };

    return transporter.sendMail(mailOptions)
        .then(info => {
            res.send("email sent");
        })
        .catch(err => {
            console.log(err);
            res.status(500).send("Error sending mail")
        })
});

See the doc

If callback argument is not set then the method returns a Promise object. Nodemailer itself does not use Promises internally but it wraps the return into a Promise for convenience. If callback is not defined for sendMail then the method returns a native Promise object instead

Ad
source: stackoverflow.com
Ad