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");
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>',
}
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
}
})
EMAIL_USER = your gmail-id
EMAIL_PASS = password
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");
}
});
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");
})
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");
})
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.
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.
Latest comments (6)
I followed your instructions and it wasn't working for me
what is the problem? any error?
I found out that the problem was from gmail.
I switched to Hotmail and it worked
good.
Highly recommended to also verify your transporter. This tests your connection and authentication before actually sending mails.
ohhh,I will add this soon.Thank you so much !🤗