gpt4 book ai didi

javascript - Nodemailer 发送带有空正文的电子邮件

转载 作者:行者123 更新时间:2023-12-03 00:13:18 25 4
gpt4 key购买 nike

我正在将 nodemailer 与 npm 包电子邮件模板一起使用。我没有收到电子邮件。当我设置预览时模板正在工作:true。控制台出现错误错误:未定义收件人。为什么会出现此错误?尝试了很多方法,但 Nodemailer 每次都发送空电子邮件。希望您理解我的问题。

标题:

from:   testing@gmail.com
to: my_email@gmail.com
date: Jan 3, 2020, 3:23 PM
subject: New password request
mailed-by: gmail.com
signed-by: gmail.com
security: Standard encryption (TLS) Learn more

错误:

Error: No recipients defined
at SMTPConnection._formatError (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
at SMTPConnection._setEnvelope (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:995:34)
at SMTPConnection.send (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:615:14)
at sendMessage (E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:227:28)
at E:\testing\node_modules\nodemailer\lib\smtp-transport\index.js:285:25
at SMTPConnection._actionAUTHComplete (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:1537:9)
at SMTPConnection.<anonymous> (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:550:26)
at SMTPConnection._processResponse (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:942:20)
at SMTPConnection._onData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
at TLSSocket.SMTPConnection._onSocketData (E:\testing\node_modules\nodemailer\lib\smtp-connection\index.js:195:44)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
code: 'EENVELOPE',
command: 'API'
}

我的目录结构:


├── app.js
└── emails
└── forget-password
├── html.pug
├── subject.pug

带有模板的 Node 邮件程序:

const nodemailer = require('nodemailer');
var generator = require('generate-password');

const Email = require('email-templates');

exports.sendNodeForgotPasswordMail = function (email, GeneratePassword) {
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'my_email',
pass: 'my_password'
},
tls: {
rejectUnauthorized: false
}

});

const emailVar = new Email({
message: {
from: 'testing@gmail.com'
},
preview: true,
send: true,
transport: {
jsonTransport: true
}
});

emailVar.send({
template: 'forget-password',
message: {
to: email
},
locals: {
password: GeneratePassword
}
}) .then(res => {
console.log('res.originalMessage', res.originalMessage)
}).catch(console.error);


return transporter.sendMail(emailVar, function (err, info) {
if (err)
console.log(err);
else
console.log(info);
});
};

Html.pug:

p Your new password is: #{password}

主题.pug

= `Password reset request`

最佳答案

看来您的项目结构不太正确。您要创建的每个模板都应该有一个目录,并且在每个目录中您应该有一个 subject.pug 文件、一个 text.pug 文件(例如仅文本)和 html.pug 文件(用于 HTML)。

文档中名为 mars 的模板的示例目录结构如下所示:

.
├── app.js
└── emails
└── mars
├── html.pug
├── text.pug
└── subject.pug

为了进行调试,您可以使用 DEBUG 环境标志:

DEBUG=email-templates node app.js

您还可以查看 emailVar.send() 的响应对象中的 originalMessage,如下所示:

emailVar.send({
...
})
.then(res => {
console.log('res.originalMessage', res.originalMessage)
})
.catch(console.error);

您可以在此处查看更多高级配置:https://www.npmjs.com/package/email-templates

关于javascript - Nodemailer 发送带有空正文的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59568560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com