How To Send Emails In Node JS | Nodemailer

Send emails in nodemailer
Table of Contents

Today I will show you how to send emails in node js it is very simple to send emails from node js we will use nodemailer which is a node js library from which we can send emails from node js for free.

You don’t need to sign up or buy any service for sending emails to your users you can implement this feature easily I will show you how to send emails in node js so read the full article carefully.

If you want some coding projects you can check out my projects I created and I have shared them on this website you can visit coderzway projects to view projects.

Let’s Send emails in node js

1 – Install Node JS

So first you need to have node js installed on your machine so head to the official node js website and download and install node js – download node js here.

You need to have atleast basic javascript and node js experience to understand the code

So Now create a folder and open it in vs code or any other editor you use. Now create a server.js file or index.js you can name it anything.

Now open the terminal or command prompt at the project location and type this

				
					npm init -y
				
			

2 – Install Nodemailer

This will initialize node project so you can install nodemailer library to do that just do npm install nodemailer in the command prompt this will install the library in your project and it will create a node modules folder

				
					npm install nodemailer

				
			

3 – Copy and paste

Now you are good to go you have everything installed and you are ready to write code you can copy and paste the below code in a file and use the send email function wherever you want to send email.

				
					const nodemailer = require('nodemailer')

const sendEmail = async (url, email) => {
    const transporter = nodemailer.createTransport({
        host: 'smtp.hostinger.in', // enter your host name
        port: 587,
        secure: false,
        auth: {
            user: 'Enter your email',
            pass: 'Enter your email password',
        }
    });
    await transporter.sendMail({
        from: 'Confirm Email <coderzway@gmail.com>',
        to: email,
        subject: 'Confirm your email',
        html: `<h1>Hello world</h1>`,
    });
}

export default sendEmail

				
			

Instructions

You can take a look at the official documentation of nodemailer here if you don’t understand – Nodemailer docs

You need to enter the name of your host in the host field if you have a email hosting account means you have a business email account you need to enter the name of the host the hosting provider will provide the host name.

Or if you don’t have a hosting account you can use gmail but gmail blocks login by robot so you need to turn of 2 FA and they have something called less secure you need allow less secure apps to login.

You need to enter your email and password at the user and pass field.

You also need to add the email you added above in the from field and you need to enter the email you want to send at to field.

you need to enter the subject of the email and in the html field you need to put the content of the email in html form.

This will send email to the user I hope you found what you were looking for and this was how to send emails in node js.

Thank you for reading till the end and if you want more articles like this then do subscibe to coderzway newsletter for daily updates of coderzway blog posts.

Share This Post
Share on facebook
Share on twitter
Share on email
Share on whatsapp

Leave a Reply

Subscribe To Our Newsletter

Get updates and learn from the best

Latest Guides & Articles