如何使用外部html文件在mailgun中发送数据?

时间:2017-02-24 05:36:00

标签: html node.js email mailgun

我使用邮件枪发送邮件。但是我不想在html字段中添加html标签,而是想创建一个单独的html文件并将其添加到那里。怎么做? (我使用Node.js表达框架)

 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 个答案:

答案 0 :(得分:2)

你可以这样做。使用fs读取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);
    }
});

此解决方案适合我。