Nodejs

Node Js – Send Mail From Localhost

Node Js - Send Mail From Localhost

Views

In this post, I will tell you,  Node Js – Send Mail From Localhost.

In this post, I am sending mail in node js from localhost with npm nodemailer package and Mailgun.

For Mailgun setup, first you need to create account on Mailgun and after create account, you need to add below information(Showing in first screenshot) into your nodejs application, that I will tell in below nodejs mail setup steps.

Node Js - Send Mail From Localhost

 

I am very happy during share this post because it helped me alot and I am hoping, this will help others too.

 

Here are the working steps you need to follow:

 

1. Very first, you need to add npm nodemailer package into your nodejs application:

Here is the command, you need to run into your terminal:

npm install nodemailer --save

 

2. Here is the working and tested code, you need to add your js file:

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'mailgun',
  auth: {
    user: 'postmaster@mgun.YourDomainName.com',
    pass: '**********************************'
  }
});

var mailOptions = {
  from: 'noreply@richpost.com',
  to: 'therichposts@gmail.com',
  subject: 'Testmail',
  text: 'Hi, mail sent.'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

// Output after run command : Email send: 250 Status code

 

3. After it, you need to run below command into your terminal:

node YourFileName.js

If everything done properly by your side then you get email like I got.

gotmailmailgun

 

If you have any query related to this post, then do comment below or ask question.

Thank you,

Happy Coding,

Therichpost

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Positive SSL