Thursday, August 14, 2008

Make your own customized GMAIL mobile Alert!

Well, not long ago a lot of services were free online ranging from Free Magazines,email accounts,hosting services and even domain names !! . Every online user just like US i.e YOU and ME wants to have a lil bit more .. after all whats harm in lil competition .Well! one such service is www.Gmail.comwww.Google.com has a long history of satisfying customers and hey! free service has no competitors ,right!! . Not to forget one more such service which is called www.160by2.com it allows people to send free 80 chars msgs anywhere in India.(rest 80 chars reserved for advt.)

 I have been a loyal user of www.Gmail.com from past few months , apart from latest developments & added features the one and only one eagerly awaited feature which is yet to launched is Gmail Mobile Alerts for India . Google has made no such official anouncement , but fortunately they will be going to offer this service soon.

Hence,My attempt here in this article is to combine two services i.e Gmail and 160by2 , so that what we have is a khichdi which ,guess wat, serves us and gives us a Customized Gtalk Mobile Alert Xclusive for India .. that too absolutely GRATIS!!

Disclaimer:Following code are not written by me from scratch . Following code belong to their respective authors which are taken from various sources such as PHP.net or other Forum(s). I do to intend to take the credit for the following code nor propose some one else ,for whom this code may be usefull, to do the same. I have just made few modification so that it serves the purpose . It does not intends to exploit the service offered by either Gmail or 160by2.

So here we go!

Requirement : Two hands (to Programm :p) , A php hosting (I use www.Freehost.ag) , knowledge of CronJob and some <?php?>

1. Attempt: To fetch the number of unread mails from Gmail account . After repeating attempt I am not able to access gmail using Imap enabled in Setting section. Hence the only alternative is to have access thru gmail atom feed which serves us well .

Class gmailCount{

function msgCount(){
$username = "Username";
$password = "Password";

// Initialise cURL
$c = curl_init('https://gmail.google.com/gmail/feed/atom');

$headers = array(
    "Host: gmail.google.com",
    "Authorization: Basic ".base64_encode($username.':'.$password),
    "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4″,
    "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5″,
    "Accept-Language: en-gb,en;q=0.5″,
    "Accept-Encoding: text",
    "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7″,
    "Date: ".date(DATE_RFC822)
);

curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($c, CURLOPT_COOKIESESSION, true);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($c, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 1);

$str = curl_exec($c);

preg_match('#<fullcount>(.*)</fullcount>#', $str, $array); // Use a regex to get what's between <fullcount>(.*)</fullcount>

return strip_tags($array[0]); //stripts all HTML Tag from the given string

curl_close($c);
}
}

Above Code uses Curl , I believe it is one of the most over-used functionality in PHP. So! basically Class gmailCount{} fetches the number of new mails from username@gmail.com and returns the number when msgCount() function is invoked from its object .

 2. Attempt : Following Code tried to establish connection with www.160by2.com

Class sendSms{

   function sendMsg($nuOfMsg){
  $username = "Username";
  $password = "Password";

  // INIT CURL
  $ch = curl_init();

  // SET URL FOR THE POST FORM LOGIN
  curl_setopt($ch, CURLOPT_URL, 'http://www.160by2.com/LoginCheck.aspx');

  // ENABLE HTTP POST
  curl_setopt ($ch, CURLOPT_POST, 1);

  // SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
  curl_setopt ($ch, CURLOPT_POSTFIELDS, 'htxt_UserName=' . $username . '&txt_Passwd=' . $password);

  // IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
  curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

  # Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
  # not to print out the results of its query.
  # Instead, it will return the results as a string return value
  # from curl_exec() instead of the usual true/false.
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  
  // EXECUTE 1st REQUEST (FORM LOGIN)
  $store = curl_exec ($ch);
  
  // SET PAGE TO SMS COMPOSE
  curl_setopt($ch, CURLOPT_URL, 'http://www.160by2.com/postview.aspx');
  

  // EXECUTE 2nd REQUEST
  $store = curl_exec ($ch);
  
  /* SET POST PARAMETERS : FORM VALUES FOR EACH FIELD. Replace 'MobileNumber. with your number with no preceding zeroes or 91.It shud be strictly 10 digit number . The Second 'MobileNumber' should also follow the same i.e write only 10 digit mobile number. */
  curl_setopt ($ch, CURLOPT_POSTFIELDS, 'txt_mobileno=MobileNumber&txt_msg=Total NEW Mails in Username@gmail is:' . $nuOfMsg . '&act_mnos=91MobileNumber');
  
  // EXECUTE 2nd REQUEST
    curl_exec ($ch);
 
  // CLOSE CURL
  curl_close ($ch);

}
}

Logs into www.160by2.com , stores the cookie so that it gives an illusion of a real browser and function sendMsg() receives the number of New mails as an argument and broadcasts it as content of the SMS . Remember , if u change the content it should NOT be more then 80 Chars as rest will be truncated.

3.Attempt : Objects are created from both the classes and a Text file is created to store the number of new mail which was fetched from the last request , the next subsequent requests deducts this number from the data stored in this file and passes this to sendMsg()

//CREATE THE NECESSARY OBJECT(S) FROM THE CLASSES
$obj=new gmailCount();
$obj2=new sendSms();

$myFile = "msgCount.txt";
$stringData = $obj->msgCount();
if(!file_exists($myFile)){
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $stringData);
$obj2->sendMsg($stringData - $stringData);
}
else {
$fh = fopen($myFile, 'r') or die("can't open file");
$theData = fread($fh, filesize($myFile));
fclose($fh);

$fh = fopen($myFile, 'w+') or die("can't open file");
fwrite($fh, $stringData);
if (($stringData - $theData) != 0) $obj2->sendMsg($stringData - $theData);
fclose($fh);

Voila! There you have it , your own customized Gmail Mobile Alert!

4. Attempt : Setup a CronJob so that this script runs after specified minutes/hours/days/weekdays/months.

If you have suggestions or Questions , feel free to drop your comments.

No comments: