Wednesday, 24 July 2013

Fetch unread mail from gmail

<?php
// to set display error to zero
         ini_set('display_errors',0);
        
//fucntion to get unread emails using username and password from function values
function check_email($username, $password)
{
    //Connect Gmail feed atom
    $url = "https://mail.google.com/mail/feed/atom";

    // Send Request to read email
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_ENCODING, "");
    $curlData = curl_exec($curl);
    curl_close($curl);
   
    //returning retrieved feed
    return $curlData;
}

//Now we can call this function with username and password as parameter.
       
 $feed = check_email("Your email", "your password");
 //print_r($feed);

/*
this will return the XML result. now we can display it as HTML.Now we can insert the email to your database also.   
*/

$x = new SimpleXmlElement($feed);

echo "<ul>";
foreach($x->entry as $entry)
{

echo '<li><p><strong>'. $entry->title.'</strong><br>';
echo $entry->summary;
echo '</p></li>';
}
echo "</ul>";

//That all.. You have done !!
      
              
?>

No comments:

Post a Comment