Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeMailer - Cannot read property 'getSocket' of undefined

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 <[email protected]>`,
    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;
        }
like image 524
aazcast Avatar asked Apr 21 '18 15:04

aazcast


1 Answers

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.

like image 83
Fostah Avatar answered Nov 04 '22 09:11

Fostah