DEV Community

Cover image for How to send email in Node.js with Nodemailer
siddharth
siddharth

Posted on

How to send email in Node.js with Nodemailer

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

require('dotenv').config();
const mailer = require("nodemailer");
Enter fullscreen mode Exit fullscreen mode

In index.js create new variable object called body and write some lines of code

let body ={
    from: 'your mail-id',
    to: 'recipient mail-id',
    subject: 'This is subject',
    html: '<h2>The html content</h2><br>',
}
Enter fullscreen mode Exit fullscreen mode

Now create transporter object that holds service and auth

const transporter =   mailer.createTransport({
    service: 'gmail',
    auth:{
        user: process.env.EMAIL_USER,
        pass : process.env.EMAIL_PASS
    }
})
Enter fullscreen mode Exit fullscreen mode
EMAIL_USER = your gmail-id
EMAIL_PASS = password
Enter fullscreen mode Exit fullscreen mode

After creating transporter object we can verify our transporter configuration with verify(callback).

// verify connection configuration
transporter.verify(function(error, success) {
  if (error) {
    console.log(error);
  } else {
    console.log("Server is ready to take our messages");
  }
});
Enter fullscreen mode Exit fullscreen mode

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

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

Full index.js code

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");
})

Enter fullscreen mode Exit fullscreen mode

Now we are ready to send code but we have left with one little task.We have to enable the setting on this LINK.Without updating this we cannot send mail.

Alt Text

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.

Top comments (6)

Collapse
 
alexanderjanke profile image
Alex Janke

Highly recommended to also verify your transporter. This tests your connection and authentication before actually sending mails.

// verify connection configuration
transporter.verify(function(error, success) {
  if (error) {
    console.log(error);
  } else {
    console.log("Server is ready to take our messages");
  }
});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
siddharth151199 profile image
siddharth

ohhh,I will add this soon.Thank you so much !🤗

Collapse
 
bonarhyme profile image
Bonaventure Chukwudi

I followed your instructions and it wasn't working for me

Collapse
 
siddharth151199 profile image
siddharth

what is the problem? any error?

Collapse
 
bonarhyme profile image
Bonaventure Chukwudi

I found out that the problem was from gmail.

I switched to Hotmail and it worked

Thread Thread
 
siddharth151199 profile image
siddharth

good.