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

JavaScript Functions

  Hi there, and welcome to another exciting lesson on JavaScript. If this is your first time here, please do well to check out our previous lessons. In the previous lesson, we were discussing JavaScript math methods. It was very intuitive as we got to learn how to use the various math methods. To proceed, we shall be looking at JavaScript functions.  A function in JavaScript like in every other programming language could be defined as a block of code that is written to perform a particular task, and this function is usually invoked or called before it is being implemented. We have been talking about methods throughout the previous lessons right. Do you know that those methods are actually functions? Yes they are functions. You can now have an overview of the importance of functions in every programming language. How do we create a function in JS? To create a function, you follow the format function functionName(Argument) {//Block of code }. Some functions do have a return val...

JavaScript Math Methods

Hello everyone, and welcome to another exciting JavaScript lesson. In the last lessons you have been seeing other methods being used in JS (for example, the string methods). We shall go further into exploring other methods. This time, it’s going to be math methods. Do not move an inch because it’s going to be a very exciting. Before we look at what a math method is, let’s have an overview of math objects. A math object in JavaScript is a static built-in object that includes properties and methods used in performing mathematical tasks. Talking about math properties, they have the syntax Math.property . Some examples are Math.E that returns Euler’s number, Math.PI that returns PI, Math.LN2 that returns the natural logarithm of 2, and many others. The various JavaScript methods contained in the math object, thus, make mathematical operations easier and reduce effort as well as time in math-oriented programming. Some JS methods include abs( ), ceil( ), cos( ), sqrt( ), pow( ), log( ) ...

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...