sinusGob
sinusGob

Reputation: 4313

How do i send and EJS template to nodemailer?

Im trying to send an ejs template to nodemailer html options

var transporter = nodemailer.createTransport(smtpTransport({
  service: 'gmail',
  host : 'smtp.gmail.com',
  secureConnection : true,
  auth : {
    user: '[email protected]',
    pass: 'abc123'
  }
}));

mailOptions = {
  to : "Your email",
  subject : "confirm your email"
  html : "HOW DO I SENT AN EJS TEMPLATE HERE?"
}

How do i pass an EJS template to the html attribute?

Upvotes: 2

Views: 3539

Answers (2)

Xcode
Xcode

Reputation: 129

Based on the answer above Since render file is asynchronous you can just have the mailOptions within the callback as below:

ejs.renderFile(__dirname + "/hello.ejs", { name: name }, function (err, data) {
    if (err) {
        console.log(err);
    } else {
        var mainOptions = {
            from: '"YOUR_NAME" YOUR_EMAIL_ADDRESS',
            to: email,
            subject: 'Account Activated',
            html: data
        };
        //console.log("html data ======================>", mainOptions.html);

        transporter.sendMail(mainOptions, function (err, info) {
          if (err) {
            res.json({
              msg: 'fail'
            })
          } else {
            res.json({
              msg: 'success'
            })
          }
      });
      }
  });

NOTE: In addition make sure the function you wrap it against is asynchronous too

Upvotes: 0

Carlos Sultana
Carlos Sultana

Reputation: 709

Use the ejs module to render your template to HTML then pass to nodemailer

var ejs = require('ejs');

var transporter = nodemailer.createTransport(smtpTransport({
  service: 'gmail',
  host : 'smtp.gmail.com',
  secureConnection : true,
  auth : {
    user: '[email protected]',
    pass: 'abc123'
  }
}));

mailOptions = {
  to : "Your email",
  subject : "confirm your email"
  html : ejs.renderFile(__dirname + '/YOUR_MAIL_TEMPLATE.ejs')
}

Upvotes: 1

Related Questions