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

I use the mail console to send letters. But instead of just adding html tags to the html field, I want to create a separate html file and add it there. How to do it? (I am using 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>' }; 
0
source share
1 answer

You can do it. Read the HTML file with fs and store in a variable. Now pass this variable email data

Try entering a 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.

+1
source

Source: https://habr.com/ru/post/1247600/


All Articles