Nodemailer - sendMail 无法读取未定义的 属性 'sendMail'

Nodemail - sendMail Cannot read property 'sendMail' of undefined

我在 docker 上的一个项目上,我想试试 nodeMail 是否有效。 但是我得到了一个错误(无法读取未定义的 属性 'sendMail')。

我尝试使用一个空灵的电子邮件进行测试。 你可以看到我把几个 console.log 甚至 console.log(this.transporter) 结果作为 "undifined" 得到 "server | 2020-04-19 14:09:50 error: error"

我不知道为什么这里有错误...

如果您有任何想法...谢谢!

import logger from 'config/logger'
import nodemailer from 'nodemailer'

/**
 * @class Mailer
 * @description Class handling Mailer operations.
 * @method sendHtmlMail
 */


export default class Mailer {

  constructor() {
    this.transporter = nodemailer.createTransport({
      host: 'smtp.ethereal.email',
      port: 587,
      secure: false,
      auth: {
        user: 'claudie.schmitt@ethereal.email',
        pass: 'DnWah2bRbw3K5bqyke'  /// i edited the password
      },
      tls: {
        rejectUnauthorized:false,
      }
    })
  }

  /**
   * @method sendHtmlMail
   * @description Send a mail
   * @param {String} to Email to
   * @param {String} subject Email subject
   * @param {String} text Email content
   * @returns {Boolean} True or False
   */
  static async sendHtmlMail({ to, subject, text }) {
    console.log("SENDING EMAIL")
    console.log(to)
    console.log(subject)
    console.log(this.transporter)
    try {
      await this.transporter.sendMail({
        from: 'claudie.schmitt@ethereal.email',
        to: 'claudie.schmitt@ethereal.email',
        subject: "MyCV created",
        html: text,
      }, (err, info) => {
        if (err) {
          console.log("NO SENT MAIL")
          logger.error('err', err)

        }
        else {
          console.log("SENT MAIL")
          logger.info('info', info)
        }
      })
    }
    catch (err) {
      logger.error(err)
      console.dir(err)
    }
    return true
  }

}

解决了没有构造函数的解决方案:

export default class Mailer {

  /**
   * @method sendHtmlMail
   * @description Send a mail
   * @param {String} to Email to
   * @param {String} subject Email subject
   * @param {String} text Email content
   * @returns {Boolean} True or False
   */
  static async sendHtmlMail({ to, subject, text }) {

    const transporter = nodemailer.createTransport({
      host: 'smtp.ethereal.email',
      port: 587,
      secure: false,
      auth: {
        user: 'ferminr51@ethereal.email',
        pass: 'QBEYfgdgdfgderGy'
      },
      tls: {
        rejectUnauthorized:false,
      }
    })

    try {
      await transporter.sendMail({
        from: 'fermin51@ethereal.email',
        to,
        subject,
        html: text,
      }, (err, info) => {
        if (err) {
          logger.error('err', err)
        }
        else {
          logger.info('info', info)
        }
      })
    }
    catch (err) {
      logger.error(err)
    }
    return true
  }

}

sendHtmlMail 定义为静态方法。

this.transporter 注册在 class 的构造函数中,它与实例相关,而静态成员独立于任何实例。

this.transporter 将在静态方法中未定义 sendHtmlMail

如果您想在静态方法中访问实例的成员,则必须将它们传入。

否则创建一个 Mailer 实例 class 并从 sendHtmlMail 方法

中删除静态
class Mailer {

    constructor() {
      this.transporter = nodemailer.createTransport({
        host: 'smtp.ethereal.email',
        port: 587,
        secure: false,
        auth: {
          user: 'claudie.schmitt@ethereal.email',
          pass: 'DnWah2bRbw3K5bqyke'  /// i edited the password
        },
        tls: {
          rejectUnauthorized:false,
        }
      })
    }

    /**
     * @method sendHtmlMail
     * @description Send a mail
     * @param {String} to Email to
     * @param {String} subject Email subject
     * @param {String} text Email content
     * @returns {Boolean} True or False
     */
    async sendHtmlMail({ to, subject, text }) {
      console.log("SENDING EMAIL")
      console.log(to)
      console.log(subject)
      console.log(this.transporter)
      try {
        await this.transporter.sendMail({
          from: 'claudie.schmitt@ethereal.email',
          to: 'claudie.schmitt@ethereal.email',
          subject: "MyCV created",
          html: text,
        }, (err, info) => {
          if (err) {
            console.log("NO SENT MAIL")
            logger.error('err', err)

          }
          else {
            console.log("SENT MAIL")
            logger.info('info', info)
          }
        })
      }
      catch (err) {
        logger.error(err)
        console.dir(err)
      }
      return true
    }

  }
  const instance = new Mailer();
  module.exports = instance;