Nodemailer and transporter

note that for A2Hosting, this.from had to be a simple email formatted in “myname@stuff.com”

module.exports = class Email {
  constructor(user, url) {
    this.to = user.email;
    this.firstName = user.firstName; 
    this.url = url;
    this.from =process.env.EMAIL_FROM;   
  }

  newTransport() { 
    if (process.env.NODE_ENV !== 'production') {
      const transporter =  nodemailer.createTransport({
        host: process.env.MYHOST,
        port: process.env.MYPORT,
        auth: {
          user: process.env.MYUSERNAME,
          pass: process.env.MYPASSWORD, 
        },
      });

      transporter.verify(function(error, success) {
        if (error) {
          console.log("Transporter" + error);
        } else {
          console.log("Server is ready to take our messages");
        }
      });
      return transporter;
    }

   

As Per the nodemailer web site

Verify SMTP connection configuration

You can verify your SMTP configuration with verify(callback) call (also works as a Promise). If it returns an error, then something is not correct, otherwise the server is ready to accept messages.


transporter.verify(function(error, success) {
  if (error) {
    console.log(error);
  } else {
    console.log("Server is ready to take our messages");
  }
});

Be aware though that this call only tests connection and authentication but it does not check if the service allows you to use a specific envelope From address or not.

Leave a Comment