How to use external html file to send data in mailgun?

 ̄綄美尐妖づ 提交于 2019-12-12 04:34:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!