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.
![How To Send An Email In Node JS How To Send An Email In Node JS](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCB2LgCwKMjxmz2Hh-lpSI75LMfA_elnWJzRM5T-1TXxAeSzT4Vx2_xEmG5TtPsK2dDkgvzgDDSf2UcTjatMhchEO3rrWEYebka_EIkUvqSOPk09MyU1qisxPe-LtuVk8_ZpYfV3EojI2G/s16000-rw/How+To+Send+An+Email+In+Node+JS.jpg)
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.
Comments
Post a Comment
Please do not enter any spam link in the comment box.