我正在使用node.js构建一个应用程序,并使用sendgrid处理我的电子邮件。有没有人知道我如何定义一个可以在电子邮件正文中使用的电子邮件模板?
提前感谢!
提前感谢!
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: "recipientmail",
from: "verifiedsendermail",
subject: "Email Subject",
template_id: "your sendgrid email dynamic template id",
personalizations: [
{
dynamic_template_data: {
fullName: "John Doe",
},
},
],
};
sgMail
.send(msg)
.then((response: any) => {
})
.catch((error: any) => {
});
Mailer.js
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey("YOUR_API_KEY");
templates = {
password_reset_confirm: "d-a02ad738dfc8404c8da016b46a7548sd",
password_reset : "d-e779dcfad71b47e7be8d79bdfe75fb0c",
confirm_account : "d-68c570dd12044d894e07566bf951964",
};
function sendEmail(data) {
const msg = {
//extract the email details
to: data.receiver,
from: data.sender,
templateId: templates[data.templateName],
//extract the custom fields
dynamic_template_data: {
name: data.name,
confirm_account_url: data.confirm_account__url,
reset_password_url: data.reset_password_url
}
};
//send the email
sgMail.send(msg, (error, result) => {
if (error) {
console.log(error);
} else {
console.log("That's wassup!");
}
});
}
exports.sendEmail = sendEmail;
Driver.js
//import the mailer.js file we previously created
var sender = require("./mailer.js");
var data = {
//name of the email template that we will be using
templateName: "confirm_account",
//sender's and receiver's email
sender: "abc@xyz.com",
receiver: "xyz@abc.com",
//name of the user
name: "Arjun Bastola",
//unique url for the user to confirm the account
confirm_account_url: "www.veniqa.com/unique_url"
};
//pass the data object to send the email
sender.sendEmail(data);