Home > front end >  How to send an auto reply email with a html page as email in NodeJS
How to send an auto reply email with a html page as email in NodeJS

Time:01-06

I recently started programming in NodeJS. I can't find any way how can I send an auto-reply email. When another person sent me an email, in that time an auto-reply email will send to his/her email, which is in HTML email format. I tried but couldn't find any way. If anyone just shows me or guides me the way, then I can do it by myself. Thanks !!

CodePudding user response:

Nodemailer is the best away to send auto mail. https://www.npmjs.com/package/nodemailer

CodePudding user response:

First you need to poll or monitor your account for the incoming mail, using a generic smtp or imap client, such as https://www.npmjs.com/package/imap, or even better, a module that actually monitors new email, such as https://www.npmjs.com/package/mail-notifier

The posted example is

Start listening new mails :

const notifier = require('mail-notifier');
 
const imap = {
  user: "yourimapuser",
  password: "yourimappassword",
  host: "imap.host.com",
  port: 993, // imap port
  tls: true,// use secure connection
  tlsOptions: { rejectUnauthorized: false }
};
 
notifier(imap)
  .on('mail', mail => console.log(mail))
  .start();
Keep it running forever :

const n = notifier(imap);
n.on('end', () => n.start()) // session closed
  .on('mail', mail => console.log(mail.from[0].address, mail.subject))
  .start();

Next, its hard to beat nodemailer to send your response.

https://nodemailer.com/about/

Here is their example. (Half this code is comments and logs)

"use strict";
const nodemailer = require("nodemailer");

// async..await is not allowed in global scope, must use a wrapper
async function main() {
  // Generate test SMTP service account from ethereal.email
  // Only needed if you don't have a real mail account for testing
  let testAccount = await nodemailer.createTestAccount();

  // create reusable transporter object using the default SMTP transport
  let transporter = nodemailer.createTransport({
    host: "smtp.ethereal.email",
    port: 587,
    secure: false, // true for 465, false for other ports
    auth: {
      user: testAccount.user, // generated ethereal user
      pass: testAccount.pass, // generated ethereal password
    },
  });

  // send mail with defined transport object
  let info = await transporter.sendMail({
    from: '"Fred Foo            
  •  Tags:  
  • Related