Saturday, July 19, 2014

Import Contacts and Invite New Contacts By Email From Gmail Contacts API - Inspired By Linkedin,Twitter Mail Importer

Inviting by E-Mail is an excellent way for asking new users to try our sites,Which is actually sent by their friends or contacts in Address Book.As Like as LinkedIn,Twitter Gets your Email address especially Gmail and Access all your contacts and Matches with Their Email Database and shows their existing users to follow(connect) and invite New users to try their respective sites.It means enjoying the site with their friends(Especially Twitter does this).Even i too joined LinkedIn and Twitter because of Invitation mail from my friends in Gmail via Twitter,LinkedIn.


Import Contacts and Invite New Contacts By Email From Gmail Contacts API - Inspired By Linkedin,Twitter Importer











Reference :  
 |  

Script Structure :
  • index.php [ Main Html markup and other scripts ]
  • mail.php   [ Send Mail with Loop for Users ]
  • invite.js    [ javascript file handling JSON data append and processing Gmail Contacts API ] 
  • process.php [ Compare exisiting users and new users responding as JSON format to frontend ]

Scope : 

This is simple add-on Application for websites having Email users database.It is similar to Twitter,Linked Contact Importer where they get API access and fetch email from corresponding service and filter them accordingly as existing users and New users.and giving some options such as Follow or Connect and Inviting by Emails.

Javascript API for Gmail Contacts Import :

Gmail API Contacts let you to Read,Update,Create Contacts in Gmail.Core part of the Contacts API below Here.

Installation and Initialization:

check here how to get those keys 
 var clientId = 'Your Client ID here';
 var apiKey = 'Your api key here';
 var scopes = 'https://www.google.com/m8/feeds';

with this we are going to add Javascript plugin Jquery,Client.js from google



<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="https://apis.google.com/js/client.js"></script>


Javascript API to get Contact List as JSON data :

Check my Last post to Authorize a Google User and access their API - Click Here

$.get("https://www.google.com/m8/feeds/contacts/default/full?alt=json&access_token=" + authResult.access_token + "&max-results=700&v=3.0",
      function(response){
if(response.feed.entry.length > 0){
         //Handle Response
       console.log(response);
         var email= [];
console.log(response.feed.entry.length);
        for ( var i= 0 ; i < response.feed.entry.length ; i++)
    {  console.log(i);
          if(response.feed.entry[i].gd$phoneNumber)
           { continue; }
else{
      email[i]= response.feed.entry[i].gd$email[0].address;
      console.log(email[i]); }
 });

Posting the Javascript email array to proccess.php via ajax to filter new users and existing users.


$.post("process.php", { email: email },
    function(Result){
     //use the results to append to show the existing users to new users.
});


process.php

<?php
header('Content-Type: application/json');
if($_POST)
{
if($_POST['email'])
{
$incominglist = $_POST['email'];
$full=array();
$con=mysqli_connect("localhost","root","shiva1995","invite");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT email,pic,name FROM email");
$full = array();
while($row = mysqli_fetch_assoc($result)) {
$full[]=$row['email'];
$fulls[]=$row['pic'];
$fulln[]=$row['name'];
}
$friendlist = array_intersect($full,$incominglist);
$piclist = array_intersect_key($fulls, $friendlist);
$namelist = array_intersect_key($fulln, $friendlist);
$friendlist = array_values($friendlist);
$piclist = array_values($piclist);
$namelist = array_values($namelist);
$newlist= array_diff($incominglist,$friendlist);
$newlist = array_values($newlist);
echo json_encode(array("newlist" =>$newlist,"users" =>$friendlist,"pic" => $piclist,"name" => $namelist));
mysqli_close($con);
}
}
view raw process.php hosted with ❤ by GitHub

process.php just filter incoming list JSON contact email to existing email database.filter them as new users as object in json ,and existing users with their prof pic we are making another object in JSON.with header JSON/Application we are just encoding output via ajax to index.php

Download the Demo files try in Localhost ,I have added Mailing code via ajax too.If you can Find any error in Javascript (invite.js) just check the console and report error as comments/ or mail me s.shivasurya@gmail.com