我正在尝试从我的Nodejs应用程序发送电子邮件。我正在使用包Nodemailer进行此操作。这是我用来发送电子邮件的代码;
let smtpTransport = nodemailer.createTransport({
host: "box***.bluehost.com",
port: 465,
secure: true,
auth: {
user: <noreply@mydomain.com>,
pass: <password>
}
});
let sendResetPasswordEmail = (user, token) => {
let mailOptions = {
to : user.workEmail,
subject : "Reset your password",
text : _generateResetPasswordEmail(user, token)
}
smtpTransport.sendMail(mailOptions, (error, response) => {
if(error){
console.error(error);
} else {
console.log("Message sent: " + response.message);
console.log(response);
}
});
}
在smtpTransport.sendMail
函数的回调中,错误为空,这是我得到的响应
{
accepted: [ 'toemail@somedomain.com' ],
rejected: [],
response: '250 OK id=short-hyphenated-alpha-numeric-id>',
envelope: { from: '', to: [ 'toemail@somedomain.com' ] },
messageId: '<long-hyphenated-alpha-numeric-id@my-machine-name>'
}
回复中的一切似乎都很好,状态代码为250,但toemail@somedomain.com
根本没有收到此电子邮件。此外,我无法在我的bluehost电子邮件仪表板中找到它&#39;已发送&#39;名单。可能出现什么问题。
答案 0 :(得分:2)
我遇到了同样的问题。我能够通过在createTransport对象上添加name属性来解决这个问题。名称:&#34; mail.yourdomain&#34;。如果您没有看到电子邮件,请检查垃圾邮件文件夹这是从https://github.com/nodemailer/nodemailer/issues/677获得的 我希望这有帮助。
答案 1 :(得分:1)
对于您的邮件选项,您在地址中丢失了。确保您的发件人地址域必须正确设置dkim和spf记录,否则大多数邮件接收者会将邮件发送到垃圾邮件或完全退回。
let mailOptions = {
to : user.workEmail,
from : "noreply@mydomain.com",
subject : "Reset your password",
text : _generateResetPasswordEmail(user, token)
}
答案 2 :(得分:0)
这是我对nodemailer的中间件设置,并且效果很好。 (我正在使用Bluthost邮件服务器)
const nodemailer = require("nodemailer");
const config = require("config");
const sendEmail = async () => {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
name: "mail.domain.com",
host: "mail.domain.com",
port: 465,
secure: true,
auth: {
user: config.get("adminEmail"),
pass: config.get("adminPass"),
},
tls: {
rejectUnauthorized: false,
},
});
transporter.verify((err, success) => {
if (err) {
console.log(err);
} else {
console.log("Server is ready to take our message");
}
});
// send mail with defined transport object
await transporter.sendMail(
{
from: '"Name You Want" <email@address.com>', // sender address
to: "reciever@email.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<h1>Hello! There?</h1>", // html body
},
(err, response) => {
if (err) {
console.log("Error: ", err);
} else {
console.log("Email sent successfully: ", response);
}
transporter.close();
}
);
};
module.exports = sendEmail;