Home > Mobile >  How to send a HTML file with Nodemailer
How to send a HTML file with Nodemailer

Time:07-15

In my node web application, i used nodemailer to sent emails.

.then(user => {
            const mailOptions = {
                from: "[email protected]",
                to: user.email,
                subject: "Reset Password",
                html: `
                    <html>
                    <head>
                        <meta charset="utf-8" />
                        <style>
                            body, * {
                                direction: ltr;
                            }
                        </style>
                    </head>
                    <body>
                        <h1>You've requested a password reset!</h1>
                        <h3>
                            Click this <a href="http://localhost:8000/new-password/${token}">Link</a> to set a new password.
                        </h3>
                    <body>
                    </html>
                `
            };
            req.flash("info", "Checkout your email for reset password link.");
            res.redirect("/products");
            transporter.sendMail(mailOptions, (error, info) => {
                if(error) console.log(error);
                else console.log(info);
            });
...

I want to send a html reversed file instead of writing all that stuff in a js file. How do i do this?

CodePudding user response:

You can just read the content from the HTML file to the variable and use this variable in mailOptions

Similar questions:

CodePudding user response:

I recommend using ejs for this purpose. It has a simple syntax that you can implement your HTML text in a ".ejs" extension file.

CodePudding user response:

Hope this is helpful to you

public static send(data) {
    const transport = nodemailer.createTransport({
      name: process.env.SMTP_HOST,
      host: process.env.SMTP_HOST,
      port: process.env.SMTP_PORT,
      auth: {
        user: process.env.SMTP_USER_NAME,
        pass: process.env.SMTP_PASSWORD,
      },
      pool: true, // use pooled connection
      rateLimit: true, // enable to make sure we are limiting
      maxConnections: 1, // set limit to 1 connection only
      maxMessages: 3, // send 3 emails per second
    });

    var mailOptions = {
      from: process.env.FROM,
      html: data.html,
      replyTo: process.env.REPLY_TO,
      to: data.to,
      subject: data.subject,
      text: data.text,
    };
    transport.sendMail(mailOptions, (error, info) => {
      if (error) {
        return console.log(error);
      }
      console.log("Message sent: %s", info.messageId);
      return;
    });
  }
  • Related