Send Mail with Node js

Send Mail with Node js

Let’s start!

Assuming you’ve already installed Node.js. For checking use node -v

Step 1

install pakage.json in your project file with this command

npm init

Step 2

For sending mail with node.js we use Nodemailer which is

(Nodemailer is a module for Node.js applications to allow easy as-cake email sending)

for more visit - nodemailer.com/about

npm install nodemailer

Step 3

File Require the nodemailer

Let's code

const nodemailer = require('nodemailer')
const transporter  = nodemailer.createTransport({
service:'gmail',
auth:{   
      user:'Your emalil', // Your Gmail ID
      pass:'Your password'  // Your Gmail Password
      }
})

transporter is going to be an object that can send mail.

Create Transporter Object: A createTransport method in nodemailer that accepts an object with some configurations and finally returns a transporter object.

// Deifne options like Sender Email and Receiver.
const mailOption = {
   from:"sender mail address",
   to:"receiver address", //also add multiple receiver 
   Subj:"",  
   text:"Helo wold",  // Mail Subject
   html: "<b>Hello world?</b>", // html body
   attachments: [
    {
      filename: 'text.txt',
      content: 'Hello world !',
      path: 'https:....' 
  }]
  }
// Send an Email 

transporter.sendMail(mailOption,(err)=>{
     if(err) console.log(err);
     else console.log(info);
 });

Deliver the message object using the sendMail() method
Open your terminal and run your project file and see the Output.

Thank you for reading my content. Be sure to follow and comment on what you want me to write about next :)

Did you find this article valuable?

Support Saurabh verma by becoming a sponsor. Any amount is appreciated!