Sunday, 21 September 2014

Sending Email using G-Mail SMTP in Node.js - Mailer and Nodemailer

Sending Email is an important Aspect for applications for notifying users,registering users and Announcement to users.However Email keeps the Application and the User interactive and stay updated for recent changes and notify.Recently i have used G-Mail for sending mails for my client,However this is Easy in PHP,Where in node.js there are two popular library to do this and send mail to your clients pragmatically from node js application.mailer is depreciated in favor of nodemailer.if you need attractive html emails better learn any templating language EJS,JADE to integrate with the email.there are seperate npm for email templates too.

Sending Email using G-Mail SMTP in Node.js - Mailer and Nodemailer


Reference : Download app Zip | mailer | nodemailer

Installation : 

  • npm install nodemailer -g
  • npm install mailer -g
these both node js modules are popular one to send mails programaticaly.Recently i utilized mailer for my cloud project.however there are lot of API to send mails like sendgrid,mailgun and lot of mail exchange service.

Mailer : 

Mailer library is also efficient to use but it has been depreciated in favor of nodemailer library in github.However it is working well, and i am using it up in my recent cloud node project.Add your gmail account/password.




Node-mailer :


here just add nodemailer and create a variable ,with configuration details creating a Transport SMTP for mail service as Gmail,and here comes authentication with Gmail username and password. 




Integration with Express :

While you could simply integrate with express app with routes.Learn here to create Express App Routes.

 var http = require("http");
 var express = require("express");
 var app = express();

  app.get("/mailer",function(res,req){

   //your mailer logic here to send mail

  }); 
  app.get("/nodemailer",function(res,req){

   //your nodemailer logic here to send mail

  });

 http.createServer(app).listen(3000);


Thus we had integrate with express and tested our Email Sending application,I havent created any fault tolerant Exceptions but sure Errors will be thrown if something goes wrong.i am leaving it in your side to handle run time Errors.


Email App Screenshots :
  • have a look at the screenshot mail sent from my Node.js app via Gmail account SMTP.
  • i have logged the request and response of Mail.
  • Mails using both mailer and node mailer library.



You could attach mail attachments in your mail.read their documentation for complete details and you coud send HTML emails by using html as parameter,however there are still Email Template from Jade,HBS and EJS templates.

Thus we have covered two Library for Sending Email from node.js that too from our own Gmail account using SMTP.have a look at the Demo File and work it out.

For Errors/bugs/comments/suggestions just comment it out or mail me s.shivasurya@gmail.com or connect with me in Facebook/Twitter/Linkedin/Google+ and chat for more in detail regarding this post and integrations.share is care.Do comments.

Saturday, 13 September 2014

Getting Started With Ionic App - Android Application

Nowadays peoples get fascinated by using apps and even my friends review many apps from play store and share their experience with their besties :D . And recently i have attended a workshop on Mobile apps development and already working with ionic apps development.However there are lot of pitfalls in using html5 with phonegap while writing for native.The only thing is easy way to code and organize front end with backend.

Getting Started With Ionic App - Android Application


Installation :  

installing ionic in your system is very easy if you have node.js installed already and integrate it with Eclipse or any other ADT supported studio with plugins.

C:\> npm install -g ionic

Learn here to install node.js and using Eclipse IDE For android development.

Configuring on Windows :

Well i don't want to write too much of configuring steps here.all is simple short list goes below
  1. you must have installed Java and accessible Via commandline(javac and java)
  2. You must have installed ANT and accessible via commandline i.e simply specify the path till ant folder create as variable ANT_HOME
  3. You must have installed eclipse IDE(i had juno) with ADT updated with Android SDK and active emulators.
  4. now once again create ENV variable ANDROID_HOME specify the path till sdk folder
  5. Create three path variable with semicolon as terminator
    %ANT_HOME%\bin;
    %ANDROID_HOME%\tools;
    %ANDROID_HOME%\platform-tools;
Make sure everything works fine via CLI.However,this is common for both Windows/Linux.If you cant traces out wrong,similar doubts are answered in Stackoverflow forum.

Creating your first App : 

create a new directory and move into it using command prompt.here we are just creating a basic app and testing it locally on emulator installing the apk file into emulator from our ionic project.

  • C:\>ionic start demoapp tabs
    this creates demoapp folder with tabs app (previously demo files from ionic)
  • C:\>cd demoapp
  • C:\demoapp> ionic platform add android
    here we just add necessary build files for specific platform,since we are doing for android add key word android.This adds necessary bin,src config source files.
  • C:\demoapp>ionic build android
    just we are going to compile android application and create apk file from our source files.
  • C:\demoapp>ionic emulate android
    and finally this is gonna create a AVD(emulators) installing the apk file and launching the current activity on screen.
  • C:\demoapp>ionic run android
    and you can install it into android mobile if you have USB debuggin connected with internal/SD card storage under developer options in android os.
If all above steps consequently works ,then u may get compiled and ready to install the app.if any errors occurs in between,it may be fault in your path setting/installation.

Errors are going to be in Node.js exception format.you can track the errors and clear it with Stackoverflow website.

Screenshots :

Create a Demoapp folder.



Ionic start demoapp tabs command just download the starter template from github account and loads into the folder for you.


ionic platform add android : creates necessary build files for android project with java files and cardova plugins.

ionic build android : compiles the source and create necessary build files for android platform and dependencies


ionic emulate android : just creates avd for you and install apk file into the emulator and launches the main activity.


ionic run android : just deploy the android application directly into your android mobile while connecting with USB , With Unknown Source apps and USB debugging connected.look at screenshot below app on emulator.


Android Sample App running active device with installed app.




Download Demo files/install and work with project.However there are lot of disadvantages in hybrid app development.Your source codes are revealed when the apk file is decompiled :D in production.


For errors/bugs/suggestion comment below or mail me s.shivasurya@gmail.com or connect with me in Facebook/Twitter/Linkedin.Share is care.

Wednesday, 3 September 2014

Login With Yahoo PHP API - OAuth2 Authentication

Yahoo is one of the network giant.I remember now once upon a time when i was learning class 3rd  (2004) I have accessed Yahoo home page from my school internet and and many websites too.btw i Dont know that was internet and Web browser . and This is just a simple post for login with yahoo API for fetching users detail for our web application and authentication.This is basic for fetching users data such as Contacts/Basic Profile and used for posting status and whatever API.We could use general API too. such as Whether forecast,Business Stocks,news headlines.however this is old API familiar to every one,I'm gonna add this and integrate with invite by email script with Google!



Resource : Download | Demo 

Installation : 

Yahoo provides a Library for their Oauth service with many inbuilt functions such as checking session and fetching users data.I have Included it in Demo File.use the Resource.


  • First of all create a developer account and login Yahoo.click here to create. 
  • Create New Application[project] for Web Oauth2 Authentication and Fetching Users Data


register your app

  • In Scope choose private data -> contacts and ur preferred options for your application



Choose private data -> contacts -> preferred read or write
  • After Final Step is to verify your Domain by uploading a file to root of the website.
  • Get your Application ID, Consumer Key,Consumer Secret and Note your Domain is verified
    and you can add details about your app in Detail section too.
final app registered


Configuration :

Under index.php just we are adding our Application details and defining ,They are used to authenticate your application and provide you the user data.
define('OAUTH_CONSUMER_KEY', 'ZZZZZZZZAAAAA');
define('OAUTH_CONSUMER_SECRET', 'AAAAAAAAA');
define('OAUTH_DOMAIN', 'mydomain.com');
define('OAUTH_APP_ID', 'APP_ID');

Authenticate The User :

Include the Yahoo library and lets make authenticate URL,we just need to pass a callback URL alone which can be defined ourself.And to maintain the Authentication flow A Javascript is introduced for User convenience , A popup for authentication and loading the page once again for API calls and fetching user data.So , We have user in_popup in our Callback url to know that the user has authenticated and to reload the page

YahooSession is a predefined class with member function to create authurl for our application by passing our key,secret and callback url

  $callback = "http://bulbon.freeiz.com/shivasurya/invite/yahoo/sample/sampleapp.php?in_popup";

  // this URL will be used for the pop-up.

  $auth_url = YahooSession::createAuthorizationUrl(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $callback);


place the $auth_url in the form of Login with Yahoo link button.

Checking for Session : 

There are two type of function to easily manage session in our application.
  1. hasSession method is used to check the web app has previous any session and this is used to determine to login once again with a popup.
  2. requireSession method is to generate a session for us/fetch session existing one and our session data consist of the profile of the user.

$hasSession = YahooSession::hasSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);

$session_new = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);

Loading user Data and Displaying Details :

So,by using the above methods we could check the existing session or request to create new session and we could load the user profile data in our web application.


if($session_check) {
    $user = $session->getSessionedUser();
  // Load the profile for the current user.
    $profile = $user->getProfile();
  }
try to var_dump the $profile array to see whole data of User who logined in to web application.For full code download and have a try in your Domain.i am leaving in your part for exception handling and checking sessions and showing login button to the users in your application.

Note : For using Yahoo API , Your domain must be verified[file upload in root directory] and above code runs in popup for authentication purpose ,enable your popups!


For Errors/comments/bugs and suggestions use comments or contact me s.shivasurya@gmail.com or connect with me via Facebook/Twitter.Share is care.