Sitemap

NODEMAILER

2 min readDec 7, 2022

--

Nodemailer

Nodemailer is a module for Node.js and that module is used for sending and receiving emails from Node.js application and its a zero dependency module and Nodemailer module is used for easily send emails as plain text, html or attachments like image, doc.pdf,…

How to send email with Nodemailer :

  1. Create package.json file
npm init -y

2. Install a nodemailer

npm install nodemailer -save

3. Create a user.js file in the project folder

step 1: import the files

step 2: html formatted

step 3: send an email to more than one receiver

step 4: you have any attachments like documents and pdf, you I’ll save project folder that is easily run the code

Get Manjuramakrishnan’s stories in your inbox

Join Medium for free to get updates from this writer.

step 5: patch means path name of the document from the folder (eg., -documents/samplenode pgm.rtf)

user.js

var nodemailer=require('nodemailer');
var sender=nodemailer.createTransport({
service:'gmail',
auth: {
user: "from emailid address",
pass: "from email id password"

}});
var composemail = {
from: 'from emailid address',
to: ['to mail id 1','to mail id 2, ... upto 100 emails '],
subject: 'send mail uing NodeJs',
content: '......',
attachments: [
{
filename:'attachment pdf name',
patch:'path name of the attachment pdf name'
}
],
html: `
<head>
<style>
h1 {text-align: center;}
img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>



<body style="background-image: url(https://medium.com/'https://files.123freevectors.com/wp-content/original/157861-white-and-gold-background-graphic.jpg');">
<h1 style="color:red;","text-align: center;">Welcome to NoodeJs</h1></br><p1 style="background-color:DodgerBlue;">Node. js is a JavaScript runtime environment that achieves low latency and high throughput by taking a “non-blocking” approach to serving requests.</p1>
<br> </br>
<br> </br>
<div1>Our Programs</div1>
<form>
<input type="radio" id="html" name="fav_language" value="HTML">
<label for="html">HTML</label><br>
<input type="radio" id="css" name="fav_language" value="CSS">
<label for="css">CSS</label><br>
<input type="radio" id="javascript" name="fav_language" value="JavaScript">
<label for="javascript">JavaScript</label>
</form>

<br> </br><img src="https://krify.co/wp-content/uploads/2019/07/Nodejs.jpg" style="width:50%;"/>
<br> </br>
<div2>Best Regards,</div2><br>


</br><div3>yourname</div3>
</body>`



};
sender.sendMail(composemail,function(error,info){
if(error)
{
console.log(error);
}
else
{
console.log("mail sended"+ info.response);
}
});

step 6: Enter the terminal

node user.js

Output of this project

Press enter or click to view image in full size
Output of Nodemailer Program

Advantages of Nodemailer:

  • It is a single module with zero dependencies.
  • It supports HTML Content.
  • Allows you to easily add attachments to messages.
  • Supports SMTP as default transport method; other supported transport methods are SES, Sendmail, Stream.
  • OAuth2 authentication.

--

--