SlideShare a Scribd company logo
NodeMailer Example:
How to Send Email
using NodeMailer
with Gmail & Mailtrap
www.bacancytechnology.com
Ways of communication have evolved
over the years. And sending email is
considered the most professional and
widely used feature in the web area.
Emails are used for interacting and
communicating with your customers so
that they don’t miss any important
updates.
Introduction
Most of all, the applications use email as their
feature for communicating with the users. Have
you ever thought about how applications send
emails? If you are looking for an answer for
your ‘how,’ then you’re at the right place!
In this tutorial, we will learn how to send emails
from the Node.JS app with the help of
NodeMailer. With the help of the NodeMailer
example, we will cover sending emails with
basic HTML content and attachments.
For setting up the fake SMTP server, we can use
Mailtrap or Gmail accounts. In this blog, we will
learn how to send email using NodeMailer with
both- Mailtrap and Gmail accounts; you can
prefer whichever you want to.
What is
NodeMailer?
Zero dependencies on other modules
Secure emails delivery
HTML content or embedded attachments
Unicode support
Various transport options besides SMTP
NodeMailer is the most famous module used for
sending and receiving emails from NodeJS
applications. It is a zero dependency module for
your NodeJS apps.
You can easily send emails as plain text, HTML,
or attachments (image, document, etc.).
Here are some of the features of NodeMailer
that makes it best and popular-
Let’s start with our NodeMailer example and
learn how to send email using NodeMailer.
NodeMailer
Example: How
to Send Email
using
NodeMailer
There are innumerable modules and packages
available for sending emails. From them,
NodeMailer is said to be the most favorable and
famous that allows you to send emails hassle-
free.
Follow this step-by-step guide for developing a
demo application and implement NodeMailer in
your NodeJS application.
Step 1: Getting Started
Create a new directory and run the npm init
command for generating the package.json file.
Use these below commands:
$ mkdir nodejs-email
$ cd nodemailer
$ npm init -y
Step-2: Install dependencies
Install the NodeMailer module using npm:
$ npm install nodemailer
Install the dotenv package to store user
credentials.
$ npm install dotenv
Create a file named .env having two variables:
EMAIL_USERNAME– For account ID
EMAIL_PASSWORD– For password
Create an app.js file in the directory for the
NodeMailer end-point. To keep it simple, we
will write the complete code in app.js.
You require the below syntax for loading
packages:
require('dotenv').config();
const nodemailer = require('nodemailer');
Step-3: Using SMTP for
Nodemailer Transport
SMTP (Simple Mail Transfer Protocol) is used to
send emails between various servers. Almost all
the email systems are SMTP-based for sending
emails over the Internet.
let transport =
nodemailer.createTransport(options[, defaults])
options – It is an object that is used to connect
with any host.
defaults – It is an object combining into each
message object. With its help, you can define
shared options, e.g., setting the default address
for email.
Nonetheless, for sending a message via our
transport, configuring the connection must.
Moving forward in our NodeMailer example.
Create a brand new Mailtrap account
Create new inbox
Get your credentials
Step:4 Connection with
Mailtrap Account
We can use Mailtrap- a dummy SMTP server.
Rather than testing the demo with your mail
and piling the inbox with test and unwanted
emails, use Mailtrap as an end-point.
If you don’t have a Mailtrap account, follow
these steps-
Later use the credentials into nodemailer’s
transport object.
let transport = nodemailer.createTransport({
host: 'smtp.mailtrap.io',
port: 2525,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
Keep your username and password inside the
.env file and use it here.
Step:5 Connection with Gmail
Account
In case you want to use your Gmail account use
the below code snippet. Keep the credentials
into the transport object.
let transport = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD
}
});
port – if secure is false, it uses 587, by default,
and 465 if true
host – it’s an IP address/hostname for
connecting to (by default to ‘localhost’)
auth – it defines authentication data
We have discussed both connections over here-
Mailtrap and Gmail. You can use any one
transport connection at a time for sending an
email.
Step: 6 Sending an Email with
Text
const mailOptions = {
from: 'sender@gmail.com', // Sender address
to: 'receiver@gmail.com', // List of recipients
subject: 'Node Mailer', // Subject line
text: 'Hello People!, Welcome to Bacancy!', //
Plain text body
};
transport.sendMail(mailOptions, function(err,
info) {
if (err) {
console.log(err)
} else {
console.log(info);
}
});
Note: Before sending an email from your Gmail
account, allow non-secure apps for accessing
your Gmail account. For that,
Go to your Gmail account settings.
Enable less secure apps for sending emails
from NodeMailer.
These are fields of the email:
from – Sender’s email address.
to – It is a comma-separated list of emails or an
array of senders’ email ID
subject – Email’s subject
text – The plaintext version of the message (
Buffer, Unicode string, Stream, or an
attachment: ({path: ‘/var/data/…’}))
Now moving towards the final section to send
an email. For that, we will use the sendMail()
method provided by the transport object that
we’ve created above.
mailOptions
callback function- It will be called when
either email is successfully sent or gives an
error.
The sendMail() method will take two
arguments:
Output- Here is the Text Email sent through
NodeMailer.
const mailOptions = {
from: 'sender@gmail.com', // Sender
address
to: 'receiver@gmail.com', // List of
recipients
subject: 'Node Mailer', // Subject line
html: '<h1 style="color:#ff6600;">Hello
People!,
Welcome to Bacancy!</h1>',
attachments: [
{ filename: 'profile.png', path:
'./images/profile.png' }
]
};
Step:7 Sending an Email with
HTML and Attachment
Use the below code snippet for sending an email
with HTML and attachments.
transport.sendMail(mailOptions, function(err,
info) {
if (err) {
console.log(err)
} else {
console.log(info);
}
});
The email contains two fields:
html – The HTML part contains a Buffer,
Unicode string, Stream, or an attachment:
({path: ‘http://…’})
attachments – An array of attachments’ objects.
Attachments can be used for embedding pdf,
images, documents, and many more.
Output- Here is the HTML Email with an
attachment sent through NodeMailer.
Conclusion
So, this was about how to send email using
NodeMailer. I hope the tutorial has assisted you
the way you have expected. You can also try
building the demo application from scratch
with us, add NodeMailer, and start learning!
And don’t forget to explore the NodeMailer
documentation.
If you’re a NodeJS enthusiast, then feel free to
visit NodeJS tutorials, where we have more
tutorials with respective Github repositories
that you can clone and play around with.
Bacancy has skilled developers with
fundamental and advanced NodeJS
development. If you are looking for
consummate NodeJS developers, then without
wasting a minute, contact Bacancy and Hire
NodeJS developers.
Thank You
www.bacancytechnology.com

More Related Content

What's hot (20)

PPTX
File system node js
monikadeshmane
 
PPTX
Angular 2.0 forms
Eyal Vardi
 
PPT
Resource Bundle
Sunil OS
 
PDF
Introduction to Rust
Jean Carlo Machado
 
PPTX
What Is Express JS?
Simplilearn
 
PPSX
Javascript variables and datatypes
Varun C M
 
PDF
File upload using multer in node.js and express.js [2021 tutorial]
Katy Slemon
 
PPTX
JavaScript Promises
L&T Technology Services Limited
 
PDF
Entity provider selection confusion attacks in JAX-RS applications
Mikhail Egorov
 
PDF
Deep dive into Coroutines on JVM @ KotlinConf 2017
Roman Elizarov
 
PDF
Vue, vue router, vuex
Samundra khatri
 
PDF
JavaScript - Chapter 11 - Events
WebStackAcademy
 
PPTX
Angular 9
Raja Vishnu
 
PDF
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?
Katy Slemon
 
PDF
Le Wagon - 2h Landing
Boris Paillard
 
PDF
HotPics 2021
neexemil
 
PDF
Gstreamer Basics
idrajeev
 
PDF
Impact of the New ORM on Your Modules
Odoo
 
PDF
Le Wagon - UI and Design Crash Course
Boris Paillard
 
PPTX
Build RESTful API Using Express JS
Cakra Danu Sedayu
 
File system node js
monikadeshmane
 
Angular 2.0 forms
Eyal Vardi
 
Resource Bundle
Sunil OS
 
Introduction to Rust
Jean Carlo Machado
 
What Is Express JS?
Simplilearn
 
Javascript variables and datatypes
Varun C M
 
File upload using multer in node.js and express.js [2021 tutorial]
Katy Slemon
 
JavaScript Promises
L&T Technology Services Limited
 
Entity provider selection confusion attacks in JAX-RS applications
Mikhail Egorov
 
Deep dive into Coroutines on JVM @ KotlinConf 2017
Roman Elizarov
 
Vue, vue router, vuex
Samundra khatri
 
JavaScript - Chapter 11 - Events
WebStackAcademy
 
Angular 9
Raja Vishnu
 
How to Build Real-time Chat App with Express, ReactJS, and Socket.IO?
Katy Slemon
 
Le Wagon - 2h Landing
Boris Paillard
 
HotPics 2021
neexemil
 
Gstreamer Basics
idrajeev
 
Impact of the New ORM on Your Modules
Odoo
 
Le Wagon - UI and Design Crash Course
Boris Paillard
 
Build RESTful API Using Express JS
Cakra Danu Sedayu
 

Similar to Node mailer example how to send email using nodemailer with gmail &amp; mailtrap (20)

PDF
Laravel mail example how to send an email using markdown template in laravel 8
Katy Slemon
 
PPTX
Mobile Application Development (Sending emails)
itstehreem2830
 
DOCX
Send email
Joselito Catanes
 
PDF
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
Katy Slemon
 
PPTX
EmaiL Parser
Harpreet Dhiman
 
PPTX
Action Mailer
SHC
 
DOC
vishal_sharma: python email sending software
vishal sharma
 
PPT
Lecture19
Châu Thanh Chương
 
PDF
Send Email In Asp.Net
Inayat Ali Jan
 
PDF
Packet Tracer WEB & Email
Mr. FM
 
DOC
Ruby
Prabhat Singh
 
DOCX
How to create mail server in cisco packet tracer
ភិក្ខុ ចន្ទបញ្ញោ ថុនសុធា
 
PPT
Lecture19
Châu Thanh Chương
 
KEY
Sending Email with Rails
James Gray
 
PPTX
Email Configuration
ProdigyView
 
PPTX
How to implement email functionalities with Mailjet api
E Boisgontier
 
PDF
Install iRedMail on Red Hat Enterprise Linux, CentOS
InfoExcavator
 
PDF
Install iRedMail on Red Hat Enterprise Linux, CentOS
Md Meherab Hossen
 
PPTX
Build restful ap is with python and flask
Jeetendra singh
 
PDF
Using Parse Server to send emails via Mandrill
Charles Ramos
 
Laravel mail example how to send an email using markdown template in laravel 8
Katy Slemon
 
Mobile Application Development (Sending emails)
itstehreem2830
 
Send email
Joselito Catanes
 
How to Set Up and Send Mails Using SendGrid in NodeJs App.pdf
Katy Slemon
 
EmaiL Parser
Harpreet Dhiman
 
Action Mailer
SHC
 
vishal_sharma: python email sending software
vishal sharma
 
Send Email In Asp.Net
Inayat Ali Jan
 
Packet Tracer WEB & Email
Mr. FM
 
How to create mail server in cisco packet tracer
ភិក្ខុ ចន្ទបញ្ញោ ថុនសុធា
 
Sending Email with Rails
James Gray
 
Email Configuration
ProdigyView
 
How to implement email functionalities with Mailjet api
E Boisgontier
 
Install iRedMail on Red Hat Enterprise Linux, CentOS
InfoExcavator
 
Install iRedMail on Red Hat Enterprise Linux, CentOS
Md Meherab Hossen
 
Build restful ap is with python and flask
Jeetendra singh
 
Using Parse Server to send emails via Mandrill
Charles Ramos
 
Ad

More from Katy Slemon (20)

PDF
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
Katy Slemon
 
PDF
Data Science Use Cases in Retail & Healthcare Industries.pdf
Katy Slemon
 
PDF
How Much Does It Cost To Hire Golang Developer.pdf
Katy Slemon
 
PDF
What’s New in Flutter 3.pdf
Katy Slemon
 
PDF
Why Use Ruby On Rails.pdf
Katy Slemon
 
PDF
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
Katy Slemon
 
PDF
How to Implement Middleware Pipeline in VueJS.pdf
Katy Slemon
 
PDF
How to Build Laravel Package Using Composer.pdf
Katy Slemon
 
PDF
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Katy Slemon
 
PDF
How to Develop Slack Bot Using Golang.pdf
Katy Slemon
 
PDF
IoT Based Battery Management System in Electric Vehicles.pdf
Katy Slemon
 
PDF
Understanding Flexbox Layout in React Native.pdf
Katy Slemon
 
PDF
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
Katy Slemon
 
PDF
New Features in iOS 15 and Swift 5.5.pdf
Katy Slemon
 
PDF
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
Katy Slemon
 
PDF
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Katy Slemon
 
PDF
Flutter Performance Tuning Best Practices From the Pros.pdf
Katy Slemon
 
PDF
Angular Universal How to Build Angular SEO Friendly App.pdf
Katy Slemon
 
PDF
Ruby On Rails Performance Tuning Guide.pdf
Katy Slemon
 
PDF
Uncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdf
Katy Slemon
 
React Alternatives Frameworks- Lightweight Javascript Libraries.pdf
Katy Slemon
 
Data Science Use Cases in Retail & Healthcare Industries.pdf
Katy Slemon
 
How Much Does It Cost To Hire Golang Developer.pdf
Katy Slemon
 
What’s New in Flutter 3.pdf
Katy Slemon
 
Why Use Ruby On Rails.pdf
Katy Slemon
 
How Much Does It Cost To Hire Full Stack Developer In 2022.pdf
Katy Slemon
 
How to Implement Middleware Pipeline in VueJS.pdf
Katy Slemon
 
How to Build Laravel Package Using Composer.pdf
Katy Slemon
 
Sure Shot Ways To Improve And Scale Your Node js Performance.pdf
Katy Slemon
 
How to Develop Slack Bot Using Golang.pdf
Katy Slemon
 
IoT Based Battery Management System in Electric Vehicles.pdf
Katy Slemon
 
Understanding Flexbox Layout in React Native.pdf
Katy Slemon
 
The Ultimate Guide to Laravel Performance Optimization in 2022.pdf
Katy Slemon
 
New Features in iOS 15 and Swift 5.5.pdf
Katy Slemon
 
How to Hire & Manage Dedicated Team For Your Next Product Development.pdf
Katy Slemon
 
Choose the Right Battery Management System for Lithium Ion Batteries.pdf
Katy Slemon
 
Flutter Performance Tuning Best Practices From the Pros.pdf
Katy Slemon
 
Angular Universal How to Build Angular SEO Friendly App.pdf
Katy Slemon
 
Ruby On Rails Performance Tuning Guide.pdf
Katy Slemon
 
Uncovering 04 Main Types and Benefits of Salesforce ISV Partnerships.pdf
Katy Slemon
 
Ad

Recently uploaded (20)

PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 

Node mailer example how to send email using nodemailer with gmail &amp; mailtrap

  • 1. NodeMailer Example: How to Send Email using NodeMailer with Gmail & Mailtrap www.bacancytechnology.com
  • 2. Ways of communication have evolved over the years. And sending email is considered the most professional and widely used feature in the web area. Emails are used for interacting and communicating with your customers so that they don’t miss any important updates.
  • 4. Most of all, the applications use email as their feature for communicating with the users. Have you ever thought about how applications send emails? If you are looking for an answer for your ‘how,’ then you’re at the right place! In this tutorial, we will learn how to send emails from the Node.JS app with the help of NodeMailer. With the help of the NodeMailer example, we will cover sending emails with basic HTML content and attachments. For setting up the fake SMTP server, we can use Mailtrap or Gmail accounts. In this blog, we will learn how to send email using NodeMailer with both- Mailtrap and Gmail accounts; you can prefer whichever you want to.
  • 6. Zero dependencies on other modules Secure emails delivery HTML content or embedded attachments Unicode support Various transport options besides SMTP NodeMailer is the most famous module used for sending and receiving emails from NodeJS applications. It is a zero dependency module for your NodeJS apps. You can easily send emails as plain text, HTML, or attachments (image, document, etc.). Here are some of the features of NodeMailer that makes it best and popular- Let’s start with our NodeMailer example and learn how to send email using NodeMailer.
  • 7. NodeMailer Example: How to Send Email using NodeMailer
  • 8. There are innumerable modules and packages available for sending emails. From them, NodeMailer is said to be the most favorable and famous that allows you to send emails hassle- free. Follow this step-by-step guide for developing a demo application and implement NodeMailer in your NodeJS application. Step 1: Getting Started Create a new directory and run the npm init command for generating the package.json file. Use these below commands: $ mkdir nodejs-email $ cd nodemailer $ npm init -y
  • 9. Step-2: Install dependencies Install the NodeMailer module using npm: $ npm install nodemailer Install the dotenv package to store user credentials. $ npm install dotenv Create a file named .env having two variables: EMAIL_USERNAME– For account ID EMAIL_PASSWORD– For password
  • 10. Create an app.js file in the directory for the NodeMailer end-point. To keep it simple, we will write the complete code in app.js. You require the below syntax for loading packages: require('dotenv').config(); const nodemailer = require('nodemailer'); Step-3: Using SMTP for Nodemailer Transport SMTP (Simple Mail Transfer Protocol) is used to send emails between various servers. Almost all the email systems are SMTP-based for sending emails over the Internet.
  • 11. let transport = nodemailer.createTransport(options[, defaults]) options – It is an object that is used to connect with any host. defaults – It is an object combining into each message object. With its help, you can define shared options, e.g., setting the default address for email. Nonetheless, for sending a message via our transport, configuring the connection must. Moving forward in our NodeMailer example.
  • 12. Create a brand new Mailtrap account Create new inbox Get your credentials Step:4 Connection with Mailtrap Account We can use Mailtrap- a dummy SMTP server. Rather than testing the demo with your mail and piling the inbox with test and unwanted emails, use Mailtrap as an end-point. If you don’t have a Mailtrap account, follow these steps- Later use the credentials into nodemailer’s transport object.
  • 13. let transport = nodemailer.createTransport({ host: 'smtp.mailtrap.io', port: 2525, auth: { user: process.env.EMAIL_USERNAME, pass: process.env.EMAIL_PASSWORD } }); Keep your username and password inside the .env file and use it here. Step:5 Connection with Gmail Account In case you want to use your Gmail account use the below code snippet. Keep the credentials into the transport object.
  • 14. let transport = nodemailer.createTransport({ host: "smtp.gmail.com", port: 465, secure: true, auth: { user: process.env.EMAIL_USERNAME, pass: process.env.EMAIL_PASSWORD } }); port – if secure is false, it uses 587, by default, and 465 if true host – it’s an IP address/hostname for connecting to (by default to ‘localhost’) auth – it defines authentication data We have discussed both connections over here- Mailtrap and Gmail. You can use any one transport connection at a time for sending an email.
  • 15. Step: 6 Sending an Email with Text const mailOptions = { from: '[email protected]', // Sender address to: '[email protected]', // List of recipients subject: 'Node Mailer', // Subject line text: 'Hello People!, Welcome to Bacancy!', // Plain text body }; transport.sendMail(mailOptions, function(err, info) { if (err) { console.log(err) } else { console.log(info); } }); Note: Before sending an email from your Gmail account, allow non-secure apps for accessing your Gmail account. For that,
  • 16. Go to your Gmail account settings. Enable less secure apps for sending emails from NodeMailer. These are fields of the email: from – Sender’s email address. to – It is a comma-separated list of emails or an array of senders’ email ID subject – Email’s subject text – The plaintext version of the message ( Buffer, Unicode string, Stream, or an attachment: ({path: ‘/var/data/…’})) Now moving towards the final section to send an email. For that, we will use the sendMail() method provided by the transport object that we’ve created above.
  • 17. mailOptions callback function- It will be called when either email is successfully sent or gives an error. The sendMail() method will take two arguments: Output- Here is the Text Email sent through NodeMailer.
  • 18. const mailOptions = { from: '[email protected]', // Sender address to: '[email protected]', // List of recipients subject: 'Node Mailer', // Subject line html: '<h1 style="color:#ff6600;">Hello People!, Welcome to Bacancy!</h1>', attachments: [ { filename: 'profile.png', path: './images/profile.png' } ] }; Step:7 Sending an Email with HTML and Attachment Use the below code snippet for sending an email with HTML and attachments.
  • 19. transport.sendMail(mailOptions, function(err, info) { if (err) { console.log(err) } else { console.log(info); } }); The email contains two fields: html – The HTML part contains a Buffer, Unicode string, Stream, or an attachment: ({path: ‘http://…’}) attachments – An array of attachments’ objects. Attachments can be used for embedding pdf, images, documents, and many more.
  • 20. Output- Here is the HTML Email with an attachment sent through NodeMailer.
  • 22. So, this was about how to send email using NodeMailer. I hope the tutorial has assisted you the way you have expected. You can also try building the demo application from scratch with us, add NodeMailer, and start learning! And don’t forget to explore the NodeMailer documentation. If you’re a NodeJS enthusiast, then feel free to visit NodeJS tutorials, where we have more tutorials with respective Github repositories that you can clone and play around with. Bacancy has skilled developers with fundamental and advanced NodeJS development. If you are looking for consummate NodeJS developers, then without wasting a minute, contact Bacancy and Hire NodeJS developers.