9

I am trying to set up a really simple contact form with nodemailer and it works fine, but my issue is that it doesn't handle errors. The page should redirect if an error is thrown, but instead the redirect does not happen and the app stops running. I cannot for the life of me figure out why this is happening. Here is my code:

if (req.method === 'POST') {
  const name = req.body.name;
  const email = req.body.email;
  const msg = req.body.message;

  const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: 'myemail', // left out here
      pass: process.env['GMAIL_PASS']
    }
  });

  const mailOptions = {
    from: 'myemail', // left out here
    to: 'myemail', // left out here
    subject: 'Portfolio Inquiry',
    text: `
          Name: ${name}
          Email: ${email}
          Message:${msg}`
  };

  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      // If an error is thrown, it should redirect back to the page with a fail message
      return res.redirect('/about?send=fail#contact');
    } else {
      return res.redirect('/about?send=success#contact');
    }
  });
}

If I introduce an error into the script by commenting out something important or just throwing an error, as I said the error handling block in the sendMail callback doesn't do anything. As I said, it does properly work and send the email, but if something went wrong I definitely want my user to know about it. Could anyone help me understand how to correct this issue?

Saeed
  • 5,413
  • 3
  • 26
  • 40
  • 1
    I can't find "res" variable here? – Kapil Raghuwanshi May 02 '20 at 16:00
  • I only included the block that handles a POST request. The `res` variable is in scope. My bad for not including the whole controller. – Michael Alexander May 02 '20 at 16:03
  • Just check this link once - https://stackoverflow.com/questions/56508261/nodemailer-email-confirmation-using-async-await – Kapil Raghuwanshi May 02 '20 at 16:06
  • I tried that solution. Didn't work. I also tried writing a wrapper function that returns false on error, else true but the function always returns false, even when the email was sent. I'm completely confused and frustrated. – Michael Alexander May 02 '20 at 16:39
  • I figured out a way to work it. If you leave out the callback it returns a promise. So I wrapped it in a function that returns the `sendMail` function without the callback, and then call the wrapper function in a `try...catch` block and that works for handling the error. – Michael Alexander May 02 '20 at 17:07

2 Answers2

6

I finally figured out a solution to this myself. Here is a wrapper function:

function sendEmail(req) {
    const name = req.body.name;
    const email = req.body.email;
    const msg = req.body.message;

    const transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        service: 'gmail',
        auth: {
            user: //left out,
            pass: process.env['GMAIL_PASS']
        }
    });

    const mailOptions = {
        from: //left out
        to: //left out
        subject: 'Portfolio Inquiry',
        text: `
Name: ${name}
Email: ${email}
Message:

${msg}`};

    return transporter.sendMail(mailOptions);
}

Then the function call:

try {
   await sendEmail(req);
   return res.redirect('/about?send=success#contact')
} catch (err) {
   return res.redirect('/about?send=fail#contact')
}

Because the sendMail function returns a promise when no callback is given, you can call it in a try...catch block.

4

As stated already:

Because the sendMail function returns a promise when no callback is given, you can call it in a try...catch block.

sendMail returns a promise, you can chain .then() and .catch()for handling like:

// async/await is not available in the global scope, so we wrap in an IIFE
(() => {
    const result = await transporter
        .sendMail(mailOptions)
        .then(console.log)
        .catch(console.error);

    // do something with `result` if needed
})();
lacy
  • 475
  • 3
  • 11