Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » Nodejs » How To Send Mail Using Nodemailer In Node Js

How To Send Mail Using Nodemailer In Node Js

How To Send Mail Using Nodemailer In Node Js

Posted on July 21, 2020December 16, 2022 By XpertPhp No Comments on How To Send Mail Using Nodemailer In Node Js

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.

Read Also: Node Js Crud Operations Example

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.

1
2
mkdir nodejs_email
cd nodejs_email

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "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.

1
npm install

Step 3: Create Server file
we 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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");
     }
});
});
See also  node js routes in separate file using express

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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: '<p>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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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");
     }
});
});

Nodejs Tags:send mail node js nodemailer, send mail via nodejs, sending email in nodejs

Post navigation

Previous Post: Laravel 7 Stripe Payment Gateway Integrate Example
Next Post: Laravel 7 Summernote Image Upload With Example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com