Send mail using Nodemailer and Node.js from a gmail account.

Sidhartha Mallick
1 min readOct 25, 2020

--

** EDIT: Currently google has disabled the secure app access feature. That renders this blog as useless. Good luck finding out other approaches.

Go to your Gmail Account settings and enable “Allow usage from third part application” . It will give different sorts of warning. Just ignore them.

Install nodemailer in your Node.Js repository

npm install nodemailer

Define a file Config.js with the following code :

module.exports = {
MAIL_ID : XXXX@gmail.com,
MAIL_PASS : XXXX
}

Define Mail.js with the following code :

var nodemailer = require("nodemailer");
const config = require('./config');
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: config.MAIL_ID,
pass: config.MAIL_PASS,
}
});
const SendCustomMail = async (email,subject, params) => {
var mailOptions = {
from:`${config.MAIL_ID}`,
to : `${email}`,
subject : `${subject}`,
html: `
<div>
<h1>Design Your HTML Email Here </h1>
<div> ${params} </div>
</div>
`
}
await smtpTransport.sendMail(mailOptions, (error, response) => {
if(error){
console.log("ERROR");
console.log(error);
} else {
console.log(response);
}
});
}
SendCustomMail(`abc@gmail.com`,`TEST MAIL`, `hello world`);

Github Link : https://github.com/ahtrahdis7/send-custom-email

--

--

Sidhartha Mallick
Sidhartha Mallick

Written by Sidhartha Mallick

building investorsync.in/, iOS@Gojek, Ex- Intern@BrowserStack

No responses yet