Php : Fetch unread gmail emails using CURL

The following code snippet will fetch all unread gmail emails of a given account in xml(atom) format.


It does this by simply fetching the page https://mail.google.com/mail/feed/atom using http authentication

curl -u gmail_username --silent "https://mail.google.com/mail/feed/atom"
/*
To use this code, fill the correct values for 
the variables $gmail_username, $gmail_password
*/

function get_gmail($username , $password)
{
	$url = "https://mail.google.com/mail/feed/atom";
	
	$c = curl_init();
	
	$options = array(
		CURLOPT_HTTPAUTH => CURLAUTH_BASIC ,
		CURLOPT_USERPWD => "$username:$password" ,
		CURLOPT_SSLVERSION => 3 ,
		CURLOPT_SSL_VERIFYPEER => FALSE ,
		CURLOPT_SSL_VERIFYHOST => 2 ,
		CURLOPT_RETURNTRANSFER => true ,
		CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" ,
		CURLOPT_URL => $url
	);
	
	curl_setopt_array($c, $options);
	$output = curl_exec($c);
	
	return $output;
}

$gmail_username = 'username';
$gmail_password = 'password';

echo get_gmail( $gmail_username , $gmail_password );






Last Updated On : 4th August 2012

Subscribe to get updates delivered to your inbox

Leave a comment