我想使用email-templates发送邮件,并在MySQL中配置传输使用sendgrid。NodeExpress 后端
然而,当我发送我的电子邮件时,我在控制台没有得到任何错误,但我没有收到任何电子邮件在我的电子邮件客户端,我已经做了额外的确保我的所有信息与API密钥和电子邮件地址是正确的。
const express = require('express');
const nodemailer = require("nodemailer")
const sendgridTransport = require("nodemailer-sendgrid-transport")
const router = express.Router();
let transporter = nodemailer.createTransport(
sendgridTransport({
auth: {
api_key:
process.env.SENDGRID_API_KEY
}
})
);
const Email = require('email-templates');
const path = require('path')
router.post('/email', (req,res) => {
const email = new Email({
message: {
from: '[email protected]'
},
transport: {
jsonTransport: transporter
}
});
email
.send({
template: path.join(__dirname, "../templates", "contact"),
message: {
to: '[email protected]'
},
locals: {
name: 'Elon'
}
})
.then(console.log)
.catch(console.error);
})
module.exports = router
链接到电子邮件模板文档https:/www.npmjs.compackageemail-templates
{
envelope: { from: '[email protected]', to: [ '[email protected]' ] },
messageId: '<[email protected]>',
message: '{"to":[{"address":"[email protected]","name":""}],"from":{"address":"[email protected]","name":""},"attachments":[],"subject":"Hi Elon, welcome to Mars","html":"\\n<p>Hi Elon,</p>\\n<p>Welcome to Mars, the red planet.</p>","text":"<hey>There.</hey>","headers":{},"messageId":"<[email protected]>"}',
originalMessage: {
to: '[email protected]',
from: '[email protected]',
attachments: [],
subject: 'Hi Elon, welcome to Mars',
html: '\n<p>Hi Elon,</p>\n<p>Welcome to Mars, the red planet.</p>',
text: '<hey>There.</hey>'
}
}
{
envelope: { from: '[email protected]', to: [ '[email protected]' ] },
messageId: '<[email protected]>',
message: '{"to":[{"address":"[email protected]","name":""}],"from":{"address":"[email protected]","name":""},"attachments":[],"subject":"Hi Elon, welcome to Mars","html":"\\n<p>Hi Elon,</p>\\n<p>Welcome to Mars, the red planet.</p>","text":"<hey>There.</hey>","headers":{},"messageId":"<[email protected]>"}',
答案其实很简单.你需要做的就是把传输配置替换成这样。
const email = new Email({
message: {
from: '[email protected]'
},
send: true,
transport:transporter
});