我在docker上的项目中,我想尝试一下NodeMail是否有效。 但是我遇到了一个错误(无法读取未定义的属性“ sendMail”)。
我试图用空灵的电子邮件进行测试。 您可以看到我放置了几个console.log甚至console.log(this.transporter)结果为“未定义” 并得到“服务器| 2020-04-19 14:09:50错误:错误”
我不知道为什么这里有错误...
如果您有任何想法...谢谢!
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
}
}
答案 0 :(得分:1)
sendHtmlMail
被定义为静态方法。
this.transporter
在类的构造函数中注册,并且与实例相关,而静态成员独立于任何实例。
this.transporter
将在静态方法sendHtmlMail
中未定义
如果要在静态方法中访问实例的成员,则必须将其传递。
否则,将创建Mailer类的实例,并从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;