Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a html file using nodemailer

I am sending emails using nodemailer, but I wanna know how to send a static HTML file from a directory.

  let transporter = nodemailer.createTransport({
      host: auth.host,
      port: auth.port,
      secure: auth.secure,
      auth: {
          type: auth.type,
          user: auth.user,
          clientId: auth.clientId,
          clientSecret: auth.clientSecret,
          refreshToken: auth.refreshToken,
          accessToken: auth.accessToken,
          expires: auth.expires
      }
  });

  let mailOptions = {
    from: '"xxxxx',
    to: 'xxxx',
    subject: "xxxx",
    text: `xxxx`,
    html: email.html
  };
like image 581
Kunal Shukla Avatar asked Mar 26 '19 12:03

Kunal Shukla


People also ask

How do I send HTML content in node JS?

Use res. sendFile instead of reading the file manually so express can handle setting the content-type properly for you. You don't need the app. engine line, as that is handled internally by express.

What is Nodemailer SMTP transport?

SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so its truly universal. Almost every email delivery provider supports SMTP based sending, even if they mainly push their API based sending.

How do I create a custom domain email in Nodemailer?

You need to configure a transporter with your custom domain info (host, port, user and password) You can find this info in the email configuration of your specific hosting provider. Hi there! I get the message 'email sent' and the function finishes with the status 'ok'.


2 Answers

You will have to read the file using the fs module.

const fs = require('fs');

const { promisify } = require('util');

const readFile = promisify(fs.readFile);

async function sendMail() {
    let transporter = nodemailer.createTransport({
      host: auth.host,
      port: auth.port,
      secure: auth.secure,
      auth: {
          type: auth.type,
          user: auth.user,
          clientId: auth.clientId,
          clientSecret: auth.clientSecret,
          refreshToken: auth.refreshToken,
          accessToken: auth.accessToken,
          expires: auth.expires
      }
  });

  let mailOptions = {
    from: '"xxxxx',
    to: 'xxxx',
    subject: "xxxx",
    text: `xxxx`,
    html: await readFile('/path/to/file', 'utf8')
  };

  // send mail
}

If the file won't change, you can cache the content.

like image 177
Marcos Casagrande Avatar answered Nov 15 '22 08:11

Marcos Casagrande


You could use fs to read your template file, and handlebars to replace your values in the template.

Here an example :

const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const handlebars = require('handlebars');
const { promisify } = require('util');
const fs = require('fs');

const readFile = promisify(fs.readFile);

smtpTransport = nodemailer.createTransport(smtpTransport({
  host: mailConfig.host,
  secure: mailConfig.secure,
  port: mailConfig.port,
  auth: {
    user: mailConfig.auth.user,
    pass: mailConfig.auth.pass
  }
}));

const sendMail = async () => {
    let html = await readFile('/path/to/file', 'utf8');
    let template = handlebars.compile(html);
    let data = {
        username: "Toto"
    };
    let htmlToSend = template(data);
    let mailOptions = {
        from: '[email protected]',
        to : '[email protected]',
        subject : 'test',
        html : htmlToSend
    };
    smtpTransport.sendMail(mailOptions, (error, info) => {
        if (error) console.log(error);
    });
});
like image 25
Hbd770 Avatar answered Nov 15 '22 07:11

Hbd770