因此,根据您所说,您创建了自己的 SMTP 服务器,这很好,值得学习,但我建议您免费使用SendinBlue 3rd 方服务,每天为您提供300 封邮件
sendinblue 入门:
所以现在您的帐户和 SMTP 服务已激活,现在的问题是您如何使用 sendinBlue?
实施:现在您已经对所有内容进行了排序,您知道密钥在哪里并登录
在 .env 文件中
sendinBlue__login=<Your email that your saw in that smtp tab>
sendinBlue__key= <Key you saw in that smtp tab>
在您要发送电子邮件的文件中
const nodemailer = require('nodemailer');
router.post('/api/email', (request, response)=>{
const transporter = nodemailer.createTransport({
service:'SendinBlue',
auth:{
user:process.env.sendinBlue__login,
pass:process.env.sendinBlue__key
}
})
const mailOptions = {
from:process.env.sendinBlue__login
to:'example@gmail.com',
subject:'Order confirmation',
html:`
<h1>Confirmation of your received order</h1>
`
}
transporter.sendMail(mailOptions, (error, info)=>{
if (error){
console.log(error);
return response.status(500).json({msg:"Email failed to send probably network problems or SMTP not activated"});
}
return reponse.status(200).json({msg:'Email sent'});
})
})
- 现在这就是 sendinBlue 的工作原理,只需简单的注册和帐户激活,如果未通过电子邮件客户支持激活,还可以激活 SMTP
如果上面的指南对您没有意义:Shameless plug
现在,如果您想使用 noreply@domain.com 和 sendinBlue 发送电子邮件,您甚至不必激活电子邮件,但仍然可以发送
- 您如何处理这就是您制作 mailOptions 的方式
router.get("/api/test", (request, response) => {
const transpoter = nodemailer.createTransport({
service: "SendinBlue",
auth: {
user: process.env.sendinBlue__email,
pass: process.env.sendinBlue__key,
},
});
const mailOptions = {
from: "noreply@Test.com",
to: "juniorWebProjects@gmail.com",
subject: "Confirmation Order",
html: `
<h1>Order Received</h1>
`,
};
transpoter.sendMail(mailOptions, (err, info) => {
if (err) {
return console.log(err);
}
return console.log("Message sent");
});
});
- 在我的 mailOptions 上查看我明确给自己写了我想要的电子邮件,当 sendinBlue 发送该电子邮件时,接收者将看到来自noreply@Test.com
