Wednesday, August 10, 2016

Send a Mail from nodeJS using GMAIL Account

Sending Mail with GMAIL using XOAUTH2

nodemailer is the most popular module for sending mail from node.js. It supports bunch of futures; SMTP, XOAuth2, XOAuth, SES, and DKIM,..etc. I write down an article to let you know how to send an email with Gmail SMTP without spam mark.

Step1 : Installation:
$> npm install nodemailer
Step2 : Register your Application at Google APIs Console
XOAuth2 is similar with OAuth2, so you need to register your application to Google. Jump to Google APIs Console, then create a project if you don't have, you will redirect to "Google APIs" tab. Select GMAIL API under Google APIs section. Click on “Enable”, you will get the notification saying “This API is enabled, but you can't use it in your project until you create credentials. Click "Go to Credentials" to do this now (strongly recommended).” Click on you will redirect to “Add credentials to your project”. In “Where will you be calling the API from?” select box select the option “Web Server(eg. Node js, Tomcat).” then select “User Data” or “Application Data” As per your requirements. Click on .
Now in next page Enter web client details. In “Authorized redirect URIs” textbox enter "https://developers.google.com/oauthplayground" if you dont have other redirect page. Click on, .
Enter Details in 3rd Step. Click on Continue.
After Clicking on Continue you will get the clientID and clientSecret. Download JSON file. Click on “Done”.
Step3 : Open Google OAuth2.0 Playground.
You will obtain refreshToken & accessToken on this step at Google OAuth2.0 Playground. Open the page from https://developers.google.com/oauthplayground, then click the gear button on the right-top. Click on “Use your own OAuth credentials” Checkbox.
Set your client ID & client secret that obtained, and select "Access token location:" as "Authorization header w/ Bearer prefix". Then close it.
On the left side of the window enter  "https://mail.google.com/" in textbox having placeholder ‘Input your own scope’. Click on “Authorize API” Button.
Step4 : Obtain the "refresh token".
Click on “Allow” on permissions page. Now in step 2 click on . You will get Access token and Refresh Token. Please save those tokens somewhere, you will need them in your code.
Step5 : Write down a snippet
Finally, it's time to write a code with node.js.


var nodemailer = require("nodemailer");

var smtpTransport = nodemailer.createTransport("SMTP", {
 service: "Gmail",
 auth: {
   XOAuth2: {
     user: "your_email_address@gmail.com", // Your gmail address.
                                           // Not @developer.gserviceaccount.com
     clientId: "your_client_id",
     clientSecret: "your_client_secret",
     refreshToken: "your_refresh_token"
   }
 }
});

var mailOptions = {
 from: "your_email_address@gmail.com",
 to: "my_another_email_address@gmail.com",
 subject: "Hello",
 generateTextFromHTML: true,
 html: "<b>Hello world</b>"
};

smtpTransport.sendMail(mailOptions, function(error, response) {
 if (error) {
   console.log(error);
 } else {
   console.log(response);
 }
 smtpTransport.close();
});

Copy above snippet, then change identifications and save the code into a file(Please make the changes wherever required).

Step6 : Send a mail
$> node mysendmail.js




2 comments:

  1. I really liked your blog, I think the content you present is fairly short but can be clearly understood. If I may give an opinion, I am a little confused with the navigation menu, I have difficulty exploring all of your content but overall I like it. Keep blogging friend !!!
    node js developer london

    ReplyDelete
  2. Baccarat: Rules of the game - Five Play Casinos
    Learn the rules kadangpintar of Baccarat with our tips for betting, including the best rules, how to win and 바카라 사이트 more! septcasino

    ReplyDelete