Send mail with nodemailer and express in node.js

Tram Ho

Hi everyone, today I will share about how to send mail with nodemailer and express in node.js, There are many articles written about this, however, when I followed, there were some errors, so today I will talk about 2 issues, the first is how to use nodemailer and the second is talking about bugs and how to fix it. Ok, let’s get started!

What is a Nodemailer?

The way I understand is simply the module that people wrote to support sending mail!

How to use it, is it complicated?

In this article, I will guide you to send gmail through nodemailer and express .. As for how to create a nodejs express program, how to fix it later (the next day) I will tell you! First, you need to install it on your project

Install nodemailer


Create gmail account

To use, sure, that is to create a gmail account (I recommend creating a new account instead of using the original account because this mail I only use to send it, and if there is later No matter what, it doesn’t matter much to your account

Security settings

For gmail, you need to install two main issues to be able to send mail, because if you do not, then you will get an error later

  1. Allow low-security applications to access: Because of security, gmail will disable this by default! Select security : Go to Less secure app access Turn on Allow access …
  2. Set IMAP access: Go to your gmail, select the settings button on the right corner of the screen In the Forwarding and POP / IMAP tab, navigate to the IMAP access Enable IMAP section

Let’s start the code!

I have the following file diagram, the file name may be different, but the function is similar

In the router file: Call nodemailer into the application

Create 2 more routers: the first is to retrieve data and the second is to process data

Above I have used flash to transfer information from the server to the user, you can use it but need to install it first! In the views file: I created an index.ejs file as follows:

Alright then! Can you test it?

Your message!

Usually I often use this function for security or to inform the necessary information for users, anything too much is not good, I think. As a user, I don’t want to have too many unimportant messages in my mailbox. You can test this function on your heroku web with the forgotten password function, how to create it, I’ll talk later! This song is a bit long 😃 ) my web is:

Share the news now

Source : Viblo