问题
I use mail-gun to send mails. But instead of just adding html tags in html field I want to create a separate html file and add it there. How to do it? (I use Node.js express framework)
var data = {
from: 'EasyFoods <postmaster@sandboxbd57df4272094073a1546c209403a45b.mailgun.org>',
to: req.user.email,
subject: 'Order is placed',
html: '<h1>You just placed an order!</h1>'
};
回答1:
You can do this.Read HTML file using fs and store in a variable, Now pass this variable email data
Try below code.
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);
}
});
This solution works for me.
来源:https://stackoverflow.com/questions/42431493/how-to-use-external-html-file-to-send-data-in-mailgun