从我的网站登录gmail

时间:2013-03-12 05:43:09

标签: php javascript jquery curl gmail

有很多类似的问题,但没有一个能够正确回答。

问题:我有gmail用户名(电子邮件ID)和密码,我想登录到gmail而没有将页面重定向到gmail.com

我试过了:

一个。获取源代码(从浏览器查看源代码),并将其复制到html页面中,     onload我在此表单中添加了用户名和密码,并使用javascript提交了此页面     =>它工作正常。

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.

湾我尝试使用cURL登录。

我能够使用以下代码获取Gmail邮件。

$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);

但是,如果厌倦了登录https://accounts.google.com/ServiceLogin的代码,它就不会登录。

$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);

问题:1。是否可以从mysite登录gmail?

  

我不想使用开放ID - 在这里我需要再次输入用户名和密码。我的用户名和密码已存储在DB中(我知道这不安全。)。所以我的要求是在点击链接时自动登录gmail。

问题:2。是否可以采用完整的(我们可以在浏览器中查看“查看源代码”)gmail的源代码(gmail的登录页面),使用php或javascript还是jQuery?

3 个答案:

答案 0 :(得分:3)

使用curl登录gmail是不可能的 - 明显的安全问题。至少我不知道怎么做。你必须使用oauth。请参阅google oauth文档。这是一个相当标准的过程,需要三次握手。

答案 1 :(得分:1)

使用Imap让这件事工作

<?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);

?>

对于Imap参考检查imap functions

答案 2 :(得分:1)

以下是我得到的答案:

<?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;
}
?>

它将显示gmail邮件的第一页=&gt;但如果你点击那里的任何链接,它将再次要求登录=&gt;因为没有为域mail.google.com设置cookie(并且无法完成 - 同源策略)