I receive many emails whose contents are of the same pattern, e.g.
Name: XXX
Phone: XXX
Bio: XXX
......
I want to write a PHP script to parse these emails and save the data into a database. I tried to use file_get_contents() to get the contents of these emails.
The problem is that, it requires authentication to access to my emails. Even though I have signed into my email account on the server (localhost actually), file_get_contents() still return the webpage that prompts me to sign in.
So how to access the contents of my emails with a PHP script?
EDIT: it is a gmail account.
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_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_URL, $gmail_url);
// not sure how to set this option
curl_setopt($ch, CURLOPT_REFERER, $login_url);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Enable imap on gmail and use the old fashion php imap library.
http://mail.google.com/support/bin/answer.py?hl=en&answer=75725
http://php.net/manual/en/book.imap.php