【发布时间】:2018-10-07 08:11:30
【问题描述】:
我尝试使用 Nodemailer 发送邮件。 Nodemailer 本身工作正常。但我也需要模板,所以我用谷歌搜索了一下,找到了email-templates。我将它添加到项目中,安装它。一切都好。我创建了一个模板,在开发模式下一切正常,但在生产模式下(在实时服务器上) 当我尝试从我的服务器发送邮件时,Nodemailer 出现以下错误:
{ Error: No recipients defined
at SMTPConnection._formatError (node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
at SMTPConnection._setEnvelope (node_modules/nodemailer/lib/smtp-connection/index.js:815:34)
at SMTPConnection.send (node_modules/nodemailer/lib/smtp-connection/index.js:431:14)
at sendMessage (node_modules/nodemailer/lib/smtp-transport/index.js:226:28)
at connection.connect (node_modules/nodemailer/lib/smtp-transport/index.js:287:21)
at SMTPConnection.once (node_modules/nodemailer/lib/smtp-connection/index.js:188:17)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at SMTPConnection.emit (events.js:208:7)
at SMTPConnection._actionEHLO (node_modules/nodemailer/lib/smtp-connection/index.js:1128:14)
at SMTPConnection._processResponse (node_modules/nodemailer/lib/smtp-connection/index.js:762:20)
at SMTPConnection._onData (node_modules/nodemailer/lib/smtp-connection/index.js:558:14)
at Socket._socket.on.chunk (node_modules/nodemailer/lib/smtp-connection/index.js:510:47)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12) code: 'EENVELOPE', command: 'API' }
错误很明显。没有定义收件人,这意味着我“错过”了 Nodemailer 的“to”参数。但我没有错过。 Nodemialer 无法从我的函数中给出的 Mailoptions(电子邮件)中检测到它。
好的,这是我的代码。
导入需要的模块
const nodemailer = require('nodemailer');
const Email = require('email-templates');
为节点创建传输器
let transporter = nodemailer.createTransport({
host: 'localhost',
port: 25,
secure: false,
tls: {
rejectUnauthorized: false
}
});
使用 email-template 创建一个新的 Mailtemplate
const email = new Email({
template: '../Services/Mail/emails/activation',
message: {
from: from,
subject: subject,
to: userEmail,
},
locals: {
username: username,
url: url
},
transport: {
jsonTransport: true,
to: userEmail,
}
});
使用 Nodemailer 发送邮件
transporter.sendMail(email, (error, info) => {
if (error) {
console.log(email);
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
});
下一个问题是,用于外部邮件渲染的 Nodemailer tutorial 已被弃用,我不知道如何修复它。有谁知道如何通过 Nodemailer 和电子邮件模板发送电子邮件。很抱歉给您带来不便。
Nodemailer 版本:4.6.4 电子邮件模板版本:3.6.0
【问题讨论】:
标签: node.js nodemailer email-templates