Monday, 24 September 2018

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"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
To use this code, fill the correct values for
the variables $gmail_username, $gmail_password
*/
 
function get_gmail($username , $password)
{
     
    $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 );

0 comments:

Post a Comment