Sending ejs template using nodemailer

前端 未结 2 1905
灰色年华
灰色年华 2021-02-01 22:40

I\'m trying to build a web application using express.js. In my application, I\'m using nodemailer for sending mail. If I just use it to send basic mail it work ok; however, when

2条回答
  •  面向向阳花
    2021-02-01 22:51

    I have updated the answer using ES6 and ES8 features, just in case anyone needs it.

    Don't forget to use it in an async function.

    const fs = require("fs");
    const nodemailer = require("nodemailer");
    const ejs = require("ejs");
    
    const transporter = nodemailer.createTransport({
      host: 'smtp.zoho.com',
      port: 465,
      secure: true,
      auth: {
        user: 'testmail@zoho.com',
        pass: '123456'
      }
    });
    
    const data = await ejs.renderFile(__dirname + "/test.ejs", { name: 'Stranger' });
    
    const mainOptions = {
      from: '"Tester" testmail@zoho.com',
      to: 'totest@zoho.com',
      subject: 'Hello, world!',
      html: data
    };
    
    transporter.sendMail(mainOptions, (err, info) => {
      if (err) {
        console.log(err);
      } else {
        console.log('Message sent: ' + info.response);
      }
    });
    

提交回复
热议问题