How to Get Email From Gmail Using PHP

In this tutorial am going to explain how to get the unread mails from your gmail account using gmail feed atom in PHP. This method using the cURL to fetch the emails from Gmail server through feed atom. XML response will return and we can convert it to HTML. You may also like How to generate short url using PHP and Google API and Using new Google reCAPTCHA using PHP

PHP Code

//Connect Gmail feed atom
$url = ""; 
// 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_ENCODING, "");
 $curlData = curl_exec($curl);
 $emails = new SimpleXmlElement($curlData);
 echo "<ul>";
 foreach($emails->entry as $entry)
  echo '<li><p>'. $entry->title.'<br>';
  echo $entry->summary;
  echo '</p></li>';
 echo "</ul>";

Leave a Reply

Your email address will not be published. Required fields are marked *