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.
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"); } }); }); |
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"); } }); }); |