如何配置Node email-template使用SendGrid作为传输方式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何配置Node email-template使用SendGrid作为传输方式相关的知识,希望对你有一定的参考价值。
我想实现的目标
我想使用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: 'info@4loop.online'
},
transport: {
jsonTransport: transporter
}
});
email
.send({
template: path.join(__dirname, "../templates", "contact"),
message: {
to: 'info@4loop.online'
},
locals: {
name: 'Elon'
}
})
.then(console.log)
.catch(console.error);
})
module.exports = router
链接到电子邮件模板文档https:/www.npmjs.compackageemail-templates
应对措施
{
envelope: { from: 'info@4loop.online', to: [ 'info@4loop.online' ] },
messageId: '<1082af04-68ec-2337-1fa0-926aca91b9ce@4loop.online>',
message: '{"to":[{"address":"info@4loop.online","name":""}],"from":{"address":"info@4loop.online","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":"<1082af04-68ec-2337-1fa0-926aca91b9ce@4loop.online>"}',
originalMessage: {
to: 'info@4loop.online',
from: 'info@4loop.online',
attachments: [],
subject: 'Hi Elon, welcome to Mars',
html: '
<p>Hi Elon,</p>
<p>Welcome to Mars, the red planet.</p>',
text: '<hey>There.</hey>'
}
}
{
envelope: { from: 'info@4loop.online', to: [ 'info@4loop.online' ] },
messageId: '<0e80bb1f-01ae-d994-ea06-549e34a0eed0@4loop.online>',
message: '{"to":[{"address":"info@4loop.online","name":""}],"from":{"address":"info@4loop.online","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":"<0e80bb1f-01ae-d994-ea06-549e34a0eed0@4loop.online>"}',
答案
答:我想用电子邮件模板发送电子邮件,并在我的NodeExpress后台配置使用sendgrid的传输。
答案其实很简单.你需要做的就是把传输配置替换成这样。
const email = new Email({
message: {
from: 'info@4loop.online'
},
send: true,
transport:transporter
});
以上是关于如何配置Node email-template使用SendGrid作为传输方式的主要内容,如果未能解决你的问题,请参考以下文章