如何使用外部 html 文件在 mailgun 中发送数据?
How to use external html file to send data in mailgun?
我用mail-gun 发邮件。但是我不想在 html 字段中添加 html 标签,而是想创建一个单独的 html 文件并将其添加到那里。怎么做? (我使用Node.js express框架)
var data = {
from: 'EasyFoods <postmaster@sandboxbd57df4272094073a1546c209403a45b.mailgun.org>',
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);
}
});
这个解决方案适合我。
我用mail-gun 发邮件。但是我不想在 html 字段中添加 html 标签,而是想创建一个单独的 html 文件并将其添加到那里。怎么做? (我使用Node.js express框架)
var data = {
from: 'EasyFoods <postmaster@sandboxbd57df4272094073a1546c209403a45b.mailgun.org>',
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);
}
});
这个解决方案适合我。