如何使用外部html文件在mailgun中发送数据?
问题描述:
我用邮箱发送邮件。但不是只是在html字段中添加html标签,我想创建一个单独的html文件并将其添加到那里。怎么做? (我用Node.js的表达框架)如何使用外部html文件在mailgun中发送数据?
var data = {
from: 'EasyFoods <[email protected]>',
to: req.user.email,
subject: 'Order is placed',
html: '<h1>You just placed an order!</h1>'
};
答
您可以在使用可变fs
和商店做this.Read HTML文件,现在把这个变量电子邮件数据
试试下面的代码。
var fs = require('fs');
var mailGun = require("mailgun-js")({
apiKey: "API-KEY",
domain:"DOMAIN-NAME"
});
var emailBody = fs.readFileSync('your html file path').toString();
var emailData = {
from: "fromEmail",
to: "toEmail",
subject: "subject",
html: emailBody
}
mailGun.messages().send(emailData, function (error, body) {
if(!error){
console.log("sendEmail : email Sent to : "+email);
}else{
console.log("sendEmail : Can not send email to : "+email+" : Error : "+error);
}
});
此解决方案适用于我。