How to send email in Node.js with Nodemailer
Tags: node, npm, javascript, tutorial
Published: 2020-09-02 14:07:36

Hello guy's, In this article we will learn how to send email in Node.js.The main advantage of this is that we does not need an API.
We just need a npm package called Nodemailer. let's start coding.

Now create new folder node-mail and open terminal in that directory and write a command 
> npm init

Hit the enter to given questions.Now install the packages that we need.
>npm install dotenv nodemailer

The Nodemailer is used to send mail and Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env.

Now create a new file named index.js and require the packages

```javascript
require('dotenv').config();
const mailer = require("nodemailer");
```
In index.js create new variable object called body and write some lines of code

```javascript
let body ={
    from: 'your mail-id',
    to: 'recipient mail-id',
    subject: 'This is subject',
    html: '<h2>The html content</h2><br>',
}
```
Now create transporter object that holds service and auth

```javascript
const transporter =   mailer.createTransport({
    service: 'gmail',
    auth:{
        user: process.env.EMAIL_USER,
        pass : process.env.EMAIL_PASS
    }
})
```

```javascript
EMAIL_USER = your gmail-id
EMAIL_PASS = password
```

After creating transporter object we can verify our transporter configuration with verify(callback).
```javascript
// verify connection configuration
transporter.verify(function(error, success) {
  if (error) {
    console.log(error);
  } else {
    console.log("Server is ready to take our messages");
  }
});
```

you just thinking about process.env. Create new file named .env and put some lines of code given below




So do the last changes index.js send mail with defined transport object



```javascript
transporter.sendMail(body,(err, result) =>{
    if (err) {
        console.log(err);
        return false
    }
    console.log(result);
    console.log("email sent");
})
```
Full index.js code
```javascript
require('dotenv').config();
const mailer = require("nodemailer");
let body ={
    from: 'your mail-id',
    to: 'recipient mail-id',
    subject: 'This is subject',
    html: '<h2>The html content</h2><br>',
}

const transporter =   mailer.createTransport({
    service: 'gmail',
    auth:{
        user: process.env.EMAIL_USER,
        pass : process.env.EMAIL_PASS
    }
})

transporter.verify(function(error, success) {
  if (error) {
    console.log(error);
  } else {
    console.log("Server is ready to take our messages");
  }
});

transporter.sendMail(body,(err, result) =>{
    if (err) {
        console.log(err);
        return false
    }
    console.log(result);
    console.log("email sent");
})

```

Now we are ready to send code but we have left with one little task.We have to enable the setting on this [LINK](https://myaccount.google.com/lesssecureapps).Without updating this we cannot send mail.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3dmyraogjajzflit4olb.png)

Now it's time to send email to the recipient. Open the terminal and write command

>node index.js

I hope it will works successfully.Let me know if you encounter any errors.