Skip to main content

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.

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...