How To Send An Email In Node JS

How To Send An Email In Node JS

Hello everyone!, In this tutorial, we will discuss how to send emails in a node JS/Express application. If you are new here, I recommend you check out our recent posts and YouTube channel for more coding videos and content.

Sending emails is a very important feature or utility in modern web applications and should be known by every developer, regardless of his programming language or framework.

Emails could be sent to confirm a user’s registration on a website, confirm payment, notify administrator on new users or important updates, for communication purposes and much more. You can how useful it is nowadays!.

In this tutorial, we will use Express and nodemailer to send emails. Nodemailer is a node third party module used in sending emails from Node JS apps.

Nodemailer has been around for some time now and has really been the best option for many Node JS developers.

Let’s write some code to send a simple email.

NB: Make ensure you have Node JS and Express installed.

To begin, we must install nodemailer module by typing the following command in our Node JS project directory.

How To Send An Email In Node JS
NB: You should have a working internet connection since doing this requires the module to be fetched from the web and be installed in your local machine.
Next, open server.js file lets set up our app to be able to send an email.
How To Send An Email In Node JS

In our example, we will use gmail service to send an email.

As you see in the code above, setting up nodemailer is really simple and easy. The following procedure should be followed.

  • firstly, set up a transporter. As the name implies, a transporter is like the service which will be used to deliver the email. In this tutorial, we used gmail. In the transporter, you also put your service authentication credentials. For example, gmail will require your email and password when setting up your nodemailer transporter.

  • After setting up your transporter, you need to add your mail options which simply refers to the information delivered inbox. For example; sender, recipient, subject, attachment and more.

  • Lastly, deliver the mail using transporter.sendMail() method which takes the mail options as parameters and a call back function.

NB: To successfully use gmail with nodemailer, you must turn ON “allow less secured apps”. Also ensure you have internet.

Now let’s run our code and see.

How To Send An Email In Node JS

How To Send An Email In Node JS

And it was successfully!

Thanks for coding with me.

Nodemailer official website:
Feel free to share your comments or worries with us.
Ask Question #trycoder
Facebook Page👉
Watch more videos here
Click to SUBSCRIBE to more Videos!

Please Like, Comment, Share and Subscribe THANK YOU!