How To Send Mail Using Nodemailer In Node Js – onlinecode

How To Send Mail Using Nodemailer In Node Js – onlinecode

In this post we will give you information about How To Send Mail Using Nodemailer In Node Js – onlinecode. Hear we will give you detail about How To Send Mail Using Nodemailer In Node Js – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this article, we would like to inform you how to send email using Nodemailer in node js. here in this example, we are using the express js and nodemailer package for send mail in node js.

Now, here we send mail in node js using SMTP. so you can see below nodemailer example.

Step 1: Create the Application Directory.First, we will open the command prompt and create the application directory in our directory. for this, you can follow the below command.

mkdir nodejs_email
cd nodejs_email

Step 2: Install node js packagesIn this step, we will create a package.json file in your application directory and paste the below code in this file.

{
  "name": "nodejs_email",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.4",
    "nodemailer": "^4.6.8"
  }
}

now, you can run below command for install packages.

npm install

Step 3: Create Server filewe will create a server.js file in your application directory and paste the below code in this file. here we will load the npm package and set up the mail authentication.

var express=require('express');
var nodemailer = require('nodemailer');

var app=express();

var transporter = nodemailer.createTransport({
 service: 'gmail',
 auth: {
        user: 'here enter your uesr id',
        pass: 'here enter your uesr password'
    }
});

app.listen(3000,function(){
console.log("Express Started on Port 3000");
});

Send Plain Email in Nodejs

If you want to send simple or plain mail then we have to use the text attribute and pass only text.

app.get('/send_plain_mail',function(req,res){
	var mailOptions = {
	  from: 'Here enter sender email address', 
	  to: 'Here enter receiver email address', 
	  subject: 'Here enter your subject', 
	  text: 'Here enter plain text'
	};
    //console.log(mailOptions);
    transporter.sendMail(mailOptions, function(error, response){
     if(error)
	 {
		//console.log(error);
        res.end("error");
     }
	 else
	 {
		//console.log("Message sent: " + response.text);
		res.end("sent");
     }
	});
});

Send Html Email in Nodejs

If you want to send a mail with HTML then we can also send HTML email in node js. so we have to use HTML attribute and pass HTML code into the HTML attribute.

app.get('/send_html_mail',function(req,res){
	var mailOptions = {
	  from: 'Here enter sender email address', 
	  to: 'Here enter receiver email address', 
	  subject: 'Here enter your subject', 
	  html: 'Here enter your html</p>'
	};
	
	
    //console.log(mailOptions);
    transporter.sendMail(mailOptions, function(error, response){
     if(error)
	 {
		//console.log(error);
        res.end("error");
     }
	 else
	 {
		//console.log("Message sent: " + response.to);
		res.end("sent");
     }
	});
});

Send Attachment Email in Nodejs

If you want to send a mail with attachment then we can also send mail with attachment in node js. so we have to use attachment attribute and pass the filename and path into attachments array.

app.get('/send_html_mail',function(req,res){
	var mailOptions = {
	  from: 'Here enter sender email address', 
	  to: 'Here enter receiver email address', 
	  subject: 'Here enter your subject', 
	  text: 'Here enter plain text',
	  attachments: [
        {
			  filename: 'Here enter your file name',
			  path: 'Here enter your file Url'
		  }
		]
	};
	
	
    //console.log(mailOptions);
    transporter.sendMail(mailOptions, function(error, response){
     if(error)
	 {
		//console.log(error);
        res.end("error");
     }
	 else
	 {
		//console.log("Message sent: " + response.text);
		res.end("sent");
     }
	});
});

Please follow and like us:

Hope this code and post will helped you for implement How To Send Mail Using Nodemailer In Node Js – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

8  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US