Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gmail unread email count using curl

I created a script that can get the unread email list as a feed from the gmail here is my code

<?php
//function to get unread emails taking username and password as parameters
function check_email($username, $password)
{ 
    //url to connect to
    $url = "https://mail.google.com/mail/feed/atom"; 

    // sendRequest 
    $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;

}

//making page to behave like xml document to show feeds
header('Content-Type:text/xml; charset=UTF-8');
//calling function
$feed = check_email("username", "password");
echo $feed;
?>

the output getting like this

<?xml version="1.0" encoding="UTF-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
<title>Gmail - Inbox for [email protected]</title>
<tagline>New messages in your Gmail Inbox</tagline>
<fullcount>1282</fullcount>
<link rel="alternate" href="http://mail.google.com/mail" type="text/html" />
<modified>2012-08-01T12:33:48Z</modified>
<entry>
<title>eBCS Pro 1 August 2012</title>
<summary>bcs logo eBCS Pro 1 August 2012 Video interview Olympic IT The Met Police&#39;s director of IT, Steve</summary>
<link rel="alternate" href="http://mail.google.com/[email protected]&amp;message_id=138e21a3404cc7b2&amp;view=conv&amp;extsrc=atom" type="text/html" />
<modified>2012-08-01T12:12:44Z</modified>
<issued>2012-08-01T12:12:44Z</issued>
<id>tag:gmail.google.com,2004:1409100718455703474</id>
<author>
<name>eBCS Newsletter</name>
<email>[email protected]</email>
</author>
</entry>
<entry>

so want to read the

<fullcount>1282</fullcount>

tag

when you pass the username and password to this function it can show the email list, I need to get only the message count, is there any way to catch or count the items

like image 748
Suneth Kalhara Avatar asked Nov 13 '22 01:11

Suneth Kalhara


1 Answers

this is the answer, i read the xml tag using php

$xmlobjc = new SimpleXMLElement($feed);

echo $xmlobjc->fullcount[0];

and replace the header with

header('Content-Type:text/html; charset=UTF-8');
like image 85
Suneth Kalhara Avatar answered Dec 18 '22 22:12

Suneth Kalhara