5

Node: v8.6.0 Nodemailer: v4.6.4

This is my code:

  const transport = nodemailer.createTransport({
  host: process.env.MAIL_HOST,
  port: process.env.MAIL_PORT,
  auth: {
    user: process.env.MAIL_USER,
    pass: process.env.MAIL_PASS
  }
});

const generateHTML = (filename, options = {}) => {
  const html = pug.renderFile(`${__dirname}/../views/email/${filename}.pug`,
    options);
  const inlined = juice(html);
  return inlined;
}

exports.send = async (options) => {
  const html = generateHTML(options.filename, options);
  const text = htmlToText.fromString(html);
  const mailOptions = {
    from: `Site <noreply@domain.com>`,
    to: options.user.email,
    subject: options.subject,
    html,
    text
  };
  const sendMail = P.promisify(transport.sendMail, transport);
  return sendMail(mailOptions);
}

When i execute sendMail i get this fail: TypeError: Cannot read property 'getSocket' of undefined↵ at sendMail (/Users/...../node_modules/nodemailer/lib/mailer/index.js:143:24

I check the mention line and is this one:

if (typeof this.getSocket === 'function') {
            this.transporter.getSocket = this.getSocket;
            this.getSocket = false;
        }
aazcast
  • 88
  • 5

2 Answers2

13

In my case, I received this error when I was trying to promisify the transport. Omit the callback parameter and it will natively return a promise. No need to promisify.

Fostah
  • 2,947
  • 4
  • 56
  • 78
-1

Try this.

   const transport = nodemailer.createTransport({
      host: process.env.MAIL_HOST,
      port: process.env.MAIL_PORT,
      auth: {
        user: process.env.MAIL_USER,
        pass: process.env.MAIL_PASS
      }
    });

    const generateHTML = (filename, options = {}) => {
      const html = pug.renderFile(`${__dirname}/../views/email/${filename}.pug`,
        options);
      const inlined = juice(html);
      return inlined;
    }

    exports.send = async (options) => {
      const html = generateHTML(options.filename, options);
      const text = htmlToText.fromString(html);
      const mailOptions = {
        from: `Site <noreply@domain.com>`,
        to: options.user.email,
        subject: options.subject,
        html,
        text
      };
      return transport.sendMail(mailOptions)
      .then((stuff) => { console.log(stuff); })
      .catch((err) => { console.log(err); }) ;

    }
Freddie
  • 736
  • 5
  • 16