2020-04-19 14:09:50 error: error"

问题描述 投票:0回答:1
I have no idea why there is an error here...

If you have any idea... thanks !

Solved the solution without the constructor :

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: '[email protected]',
        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: '[email protected]',
        to: '[email protected]',
        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
  }

}

I'm on a project that is on docker and i wanted to try if the nodeMail is effective. But i got an error (Cannot read property 'sendMail' of undefined). I tried to use a ethereal email to test. You ...

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: '[email protected]',
        pass: 'QBEYfgdgdfgderGy'
      },
      tls: {
        rejectUnauthorized:false,
      }
    })

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

}
is defined as a static method.
node.js nodemailer
1个回答
1
投票

sendHtmlMailIf you want to access the members of an instance within a static method then you have to pass them in.

this.transporterElse create an instance of the Mailer class and drop static from the

this.transporter methodsendHtmlMail

我在一个docker上的项目上,我想试试nodeMail是否有效,但我得到了一个错误(Cannot read property 'sendMail' of undefined)。sendHtmlMail我试着用一个ethereal邮件来测试.你可以看到我把几个控制台.日志,甚至控制台.日志(this.transporter)的结果为 "undifined",并得到 "server

© www.soinside.com 2019 - 2024. All rights reserved.