1

There are lot of similar questions, but none of them answered properly.

Issue : I have gmail username(email id) and password, I want to login to gmail with out redirecting the page to gmail.com

I tried :

a. Fetched the source code (View source from browser), and copied it in a html page, onload I added username and password to this form and submitted this page with javascript => It works fine.

But, I need to fetch the source dynamically, using PHP.

So, I tried with `file_get_contents` => Here I am not able to get full source code some of the js code and some hidden fields are missing => When I tired to login with this code, it is not logging in, it simply redirects to gmail.com login page.

Then I tried to get the source code using cURL, This also gave me incomplete source, and I am not able to login.

b. I tried to login using cURL.

I was able to get the gmail feed using following code.

$email    = 'username@gmail.com';
$password = 'password';
$curl     = curl_init('https://mail.google.com/mail/feed/atom');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $email.':'.$password);
echo $content  = curl_exec($curl);

But, When tired the code to login to https://accounts.google.com/ServiceLogin it is not loging in.

$email    = 'username@gmail.com';
$password = 'password';
$curl     = curl_init('https://accounts.google.com/ServiceLogin');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $email.':'.$password);
echo $content  = curl_exec($curl);

Question : 1. Is it possible to login to gmail, from mysite ?

I don't want to use open ID - Here I need to enter username and password again. My username and password in already stored in DB (I know this is not secure.). So my requirement is while clicking a link automatically login to gmail.

Question : 2. Is it possible to take take the complete (Which we can see in browser "view source") source code of gmail(Login page of gmail), either using php or javascript or jQuery ?

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
  • 1
    cURL is good for static page. the Gmail page is full of JS so, indeed, you will not be able to retrieve the full content. You should have a context to run javascript code before. – MatRt Mar 12 '13 at 05:45
  • 1
    http://stackoverflow.com/questions/4874186/login-to-site-using-gmail same question is here You are doing very unsecured way to achieve this. – chhameed Mar 12 '13 at 05:47
  • @chhameed : I know that, my question is different, I don't want open ID – Prasanth Bendra Mar 12 '13 at 05:49
  • 1
    With https://accounts.google.com/ServiceLogin you will have to submit some POST data, it won't login using a simple CURLOPT_USERPWD. – Havenard Mar 12 '13 at 05:49
  • @user1073122 : "You should have a context to run javascript code before." how can i do that ? – Prasanth Bendra Mar 12 '13 at 11:44

3 Answers3

3

It's not possible to login to gmail using curl - obvious security issues. At least I'm not sure how. You'll have to use oauth. Please refer to google oauth documentation. It's a fairly standard process requiring a three way handshake.

Pradeep Banavara
  • 983
  • 2
  • 11
  • 33
1

Use Imap to get this thing working

<?php

$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'yourmail@gmail.com';
$password = 'yourpass';


$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

//If connected search for unread mails or do your stuff using imap functions

$emails = imap_search($inbox,'UNSEEN');

if(count($emails) > 0) {
  foreach($emails as $email)
   $status = imap_setflag_full($inbox, $email, "\\Seen \\Flagged");

echo gettype($status) . "\n";
echo $status . "\n";
} 

imap_close($inbox);

?>

For Imap reference check imap functions

Nirmal Ram
  • 1,722
  • 4
  • 25
  • 45
  • 1
    Here You will only get the contents of gmail, but is not actually loged in to gmail...if you take the link in browser it will again ask for login. anyway +1 for your effort. – Prasanth Bendra Mar 12 '13 at 06:17
1

Here is the answer which i got :

<?php

$USERNAME = 'user@gmail.com';
$PASSWORD = 'pass@gmail';
$COOKIEFILE = 'cookies.txt';

$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE);
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLogin');
$data = curl_exec($ch);

//echo $data;

$formFields = getFormFields($data);

//print_r($formFields);

$formFields['Email']  = $USERNAME;
$formFields['Passwd'] = $PASSWORD;
unset($formFields['PersistentCookie']);

$post_string = '';
foreach($formFields as $key => $value) {
    $post_string .= $key . '=' . urlencode($value) . '&';
}

$post_string = substr($post_string, 0, -1);

curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_REFERER, 'https://mail.google.com/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($ch);
/*
if (strpos($result, '<title>Redirecting') === false) {
    die("Login failed");
    var_dump($result);
} else {*/
    curl_setopt($ch, CURLOPT_URL, 'https://mail.google.com/mail/h/jeu23doknfnj/?zy=e&f=1');
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, null);

    $result = curl_exec($ch);
    //header('Location:https://mail.google.com/mail/h/jeu23doknfnj/?zy=e&f=1');
    var_dump($result);
//}

function getFormFields($data)
{
    if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
        $inputs = getInputs($matches[1]);

        return $inputs;
    } else {
        die('didnt find login form');
    }
}

function getInputs($form)
{
    $inputs = array();

    $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);

    if ($elements > 0) {
        for($i = 0; $i < $elements; $i++) {
            $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);

            if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
                $name  = $name[1];
                $value = '';

                if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
                    $value = $value[1];
                }

                $inputs[$name] = $value;
            }
        }
    }

    return $inputs;
}
?>

It will show you the first page the gmail mails=> but if you click any link there it will again ask for login =>Because cookies are not set for domain mail.google.com (And can not be done - Same origin policy)

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73