NodeMailer - 无法读取属性' getSocket'未定义的

时间:2018-04-21 15:24:10

标签: node.js nodemailer

节点:v8.6.0 Nodemailer:v4.6.4

这是我的代码:

  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);
}

当我执行sendMail时,我得到了这个失败: TypeError:无法读取属性&#39; getSocket&#39; of sendMail的undefined↵(/ Users /...../ node_modules / nodemailer / lib / mailer / index.js:143:24

我检查了提及行,就是这个:

if (typeof this.getSocket === 'function') {
            this.transporter.getSocket = this.getSocket;
            this.getSocket = false;
        }

2 个答案:

答案 0 :(得分:5)

在我的情况下,当我尝试限制传输时收到此错误。 省略回调参数,它会自然返回一个promise。无需承诺。

答案 1 :(得分:-1)

试试这个。

   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); }) ;

    }