How To Send email In NodeJs

How To Send email In NodeJs

In this post we will give you information about How To Send email In NodeJs. Hear we will give you detail about How To Send email In NodeJsAnd how to use it also give you demo for it if it is necessary.

Today, Laravelcode share with you how to send mail in nodejs. in current position nodejs is more and more use. many things we have done with nodejs without write huge code.

In this tutorials we are share with you email sending in nodejs. becasue in any application we must be required a mail sending functionality.

We are write here very simple code for mail sending in nodejs. you can mail send in nodejs using nodemailer

Step : 1 Install nodemailer

We are first need to install nodemailer package for mail sending in nodejs. run followign command for install it.


npm install [email protected]

Step : 2 Create mail.js file

[ADDCODE]

No create mail.js file in your project root directory and simple put following code into it.


var nodemailer = require('nodemailer');

// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
        service: 'Gmail',
        auth: {
            user: "[email protected]",
            pass: "onlinecode"
        }
    });

console.log('SMTP Configured');

// Message object
var message = {

    // sender info
    from: 'Sender Name <[email protected]>s',

    // Comma separated list of recipients
    to: '"Receiver Name" <[email protected]>s',

    // Subject of the message
    subject: 'How to send mail in nodejs ✔', 

    // plaintext body
    text: 'Hello, everyone!',

    // HTML body
    html:'<p>s<b>sLook this</b>s  <img src=""/>s</p>s'+
         '<p>sHere i am send my picture attachment:<br/>s</p>s'
};

console.log('Sending Mail');
transport.sendMail(message, function(error){
  if(error){
      console.log('Error occured');
      console.log(error.message);
      return;
  }
  console.log('Message sent successfully!');

  // if you don't want to use this transport object anymore, uncomment following line
  //transport.close(); // close the connection pool
});

Now we are ready to run our example so run bellow command ro quick run:


node mail.js

If you face any problem then please write a comment or give some suggestions for improvement. Thanks…

Hope this code and post will helped you for implement How To Send email In NodeJs. 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

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