NodeJS | Send email including from, to, cc and bcc fields using nodemailer in just 3 steps

Published: 30 November 2019
on channel: Technical Babaji (Tarique Akhtar)
4,979
60

Sending an email in nodejs is a breeze thanks to NodeMailer. Let me walk you through the process of sending an email using NodeMailer.
firstly install nodemailer in your node application.



npm install --save nodemailer



after installing the nodemailer, in your main node.js file require the nodemailer
var nodemailer = require('nodemailer');
nodemailer needs a transport service using which it can send emails. In this example I am using gmail.

inside your app.js file write
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'yourpassword'
}
})
We used gmail as our transport service.
The next up is our auth object in which we have to specify our email address and password of gmail for allowing nodemailer to login and send email using our gmail account.
Now we need a second configuration object where we will be configuring our email details.


const mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: 'Subject of your email', // Subject line
html: 'Your html here'// plain text body
};

Most important four options are :-

from : The sender of the email

to : The recipient of the email

subject : the subject of the email

html : all the magic happens here

Before sending your email using gmail you have to allow non secure apps to access gmail you can do this by going to your gmail settings here.


Watch video NodeJS | Send email including from, to, cc and bcc fields using nodemailer in just 3 steps online without registration, duration hours minute second in high quality. This video was added by user Technical Babaji (Tarique Akhtar) 30 November 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 4,979 once and liked it 60 people.