Nodejs + Nodemailer @4.6.4 + Emailtemplates @3.6.0
Nodejs +Nodemailer @4.6.4 + Emailtemplates @3.6.0
我尝试用 Nodemailer 发送邮件。 Nodemailer itself works fine. But I also need Templates, so I googled a bit about it and found 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' }
错误很明显。没有定义收件人,这意味着我 "missed" 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
}
});
使用电子邮件模板创建新的邮件模板
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
您不需要使用单独的Nodemailer,因为email-templates 已经集成了Nodemailer 本身。因此,为了能够使用 Nodemailer 发送电子邮件,只需使用 Nodemailer 传输配置对象设置传输选项,并启用电子邮件模板的发送选项。总而言之,您的代码应如下所示:
const email = new Email({
template: '../Services/Mail/emails/activation',
message: {
from: from,
subject: subject,
to: userEmail,
},
locals: {
username: username,
url: url
},
send: true,
transport: {
host: 'localhost',
port: 25,
secure: false,
tls: {
rejectUnauthorized: false
}
}
});
之后,调用email-template的send函数(其文档中:https://email-templates.js.org/#/):
email.send({
template: 'mars',
message: {
to: 'elon@spacex.com'
},
locals: {
name: 'Elon'
}
})
.then(console.log)
.catch(console.error);
我尝试用 Nodemailer 发送邮件。 Nodemailer itself works fine. But I also need Templates, so I googled a bit about it and found 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' }
错误很明显。没有定义收件人,这意味着我 "missed" 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
}
});
使用电子邮件模板创建新的邮件模板
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
您不需要使用单独的Nodemailer,因为email-templates 已经集成了Nodemailer 本身。因此,为了能够使用 Nodemailer 发送电子邮件,只需使用 Nodemailer 传输配置对象设置传输选项,并启用电子邮件模板的发送选项。总而言之,您的代码应如下所示:
const email = new Email({
template: '../Services/Mail/emails/activation',
message: {
from: from,
subject: subject,
to: userEmail,
},
locals: {
username: username,
url: url
},
send: true,
transport: {
host: 'localhost',
port: 25,
secure: false,
tls: {
rejectUnauthorized: false
}
}
});
之后,调用email-template的send函数(其文档中:https://email-templates.js.org/#/):
email.send({
template: 'mars',
message: {
to: 'elon@spacex.com'
},
locals: {
name: 'Elon'
}
})
.then(console.log)
.catch(console.error);