ADVERTISEMENT
ADVERTISEMENT

Home » Node.js

How to send emails with Nodemailer using HTML as content | Node.js

Sending emails using Nodemailer: Here, we are going to learn how to send emails with attachments using Nodemailer using HTML as content in Node.js?
Submitted by Godwill Tetah, on September 15, 2019

Prerequisite:

  1. How to send emails using Nodemailer | Node.js
  2. How to send emails with attachments using Nodemailer | Node.js

This time around, we will see how to compose or build/format our email body using HTML?

Code:

// load the node mailer module
var nodemailer = require('nodemailer');    

//configure the transporter

var transporter = nodemailer.createTransport({    
  service: 'gmail',
  auth: {
    user: '[email protected]',
    pass: 'your gmail password'
  }
});

//email  options

var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Sending Email using Node.js',
  html: '<h1> Node JS </h1> <br> <h5> Hello World</h5>'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent');
  }
});

Note: You can use the same format and send as many attachments as possible. Make sure the closing curly bracket of each attachment is separated by a comma sign (,).

Finally, start your node app and if sending is successful, the phrase ‘email sent' will be printed out on the console or terminal.

Check you're the email's inbox.

Using Gmail as your transporter, you can also enable the less secure app access setting.

Nodemailer 1

Sending email requires internet connection.

Do not fear about your password security. It's a tested and secured module used by many since 2010.

Nodemailer 2

Nodemailer 3

Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.