I'm trying to load my email page, logged in, with php file_get_contents. Unsurprisingly, that isn't working, so would it be possible to edit the stream with php and place the account credentials in it to be able to log me in? Here is an article I've read on editing streams in php, but I really don't understand what they're doing.
-
http://stackoverflow.com/questions/7732634/making-a-http-get-request-with-http-basic-authentication – Marc B Jun 19 '14 at 18:28
1 Answers
If you are interested in reading your email, downloading the webmail with a file_get_contets call might not be the better way.
If you are using Gmail or any other known mailing service, you should give a try with IMAP or POP3 protocols as they will surely give you the option.
Here you have a sample piece of code for accessing Gmail through IMAP (previously you need to activate IMAP access from your Gmail settings).
<?php
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'username@gmail.com';
$password = 'password';
$inbox = imap_open($hostname,$username,$password) or die('Error: ' . imap_last_error());
$emails = imap_search($inbox, 'UNSEEN');
if($emails) {
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1);
echo $overview[0]->subject."\n";
echo $message;
}
}
imap_close($inbox);
In case you CAN'T use IMAP or POP3 because your mail provider does not allow you such access methods, what you will need is to pass the session identifier of your current connection through the HTTP headers. When you are doing the file_get_contents() call, you are not getting your mail because you are not connected on that request.
Third optional parameter on file_get_contents allow you to give a context, which might include the HEADERS required to keep the session on your new request.
-
-
I never did it but it seems a little complicated for me, are you sure about not trying with IMAP or POP3? You can take a look at the stackoverflow answer that Marc added as a comment to your question: http://stackoverflow.com/questions/7732634/making-a-http-get-request-with-http-basic-authentication – aaronfc Jun 19 '14 at 21:27
-