2

I want to get the contents of some emails in my gmail account. I would like to use the PHP cURL extension to do this. I followed these steps in my first try:

  1. In the PHP code, output the contents of https://www.google.com/accounts/ServiceLoginAuth.
  2. In the browser, the user input username and password to login.
  3. In the PHP code, save cookies in a file named cookie.txt.
  4. In the PHP code, send request to https://mail.google.com/ along with cookies retrieved from cookie.txt and output the contents.

The following code does not work:

$login_url = 'https://www.google.com/accounts/ServiceLoginAuth';
$gmail_url = 'https://mail.google.com/';
$cookie_file = dirname(__FILE__) . '/cookie.txt';

$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_URL, $login_url);
$output = curl_exec($ch);
echo $output;

curl_setopt($ch, CURLOPT_URL, $gmail_url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$output = curl_exec($ch);
echo $output;

curl_close($ch);
powerboy
  • 10,523
  • 20
  • 63
  • 93
  • Why doesn't it work? What happens? – SLaks May 16 '10 at 22:12
  • @Slaks: after clicking the "sign in" button, the browser leave localhost and direct to https://www.google.com/accounts/ServiceLoginAuth. Therefore, no useful cookies is saved in cookie.txt – powerboy May 16 '10 at 22:24

1 Answers1

4

Your approach is wrong. You cannot retrieve the contents of https://www.google.com/accounts/ServiceLoginAuth and output it, expect the user to fill in the details and press login. Since the form is defined as

<form action="https://www.google.com/accounts/ServiceLoginAuth" method="post">

the login details will be submitted by the browser to that page and your script never get hold of the cookies. You need to submit a post request to https://www.google.com/accounts/ServiceLoginAuth already with the username and password. Only then curl will receive a response with the cookies.

That said, I'd suggest you scrape all this, enable IMAP in GMail and use that to access your e-mails.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • So you mean I have to hard-coding username and password? – powerboy May 16 '10 at 22:54
  • And I did not use IMAP because I may need to access other google services such as calendar. – powerboy May 16 '10 at 22:55
  • No. You can show a form with a username and password field whose action is your e-mail-fetching-script. Then you would read the submitted data and configure curl to send them as POST data. – Artefacto May 16 '10 at 22:56
  • Google has APIs to use those servers. You ought to use them, this approach may break any time Google changes something in its pages. See http://code.google.com/apis/calendar/data/2.0/developers_guide.html – Artefacto May 16 '10 at 22:58
  • @Artefacto: but I noticed that the form in https://www.google.com/accounts/ServiceLoginAuth contains some hidden inputs whose values are randomly generated, e.g., – powerboy May 16 '10 at 23:00
  • Then before submitting the POST request, make a GET request to retrieve those values and then, when making the POST request, include them alongside the user name and password. – Artefacto May 16 '10 at 23:04