duanquan4451 2014-06-19 18:23
浏览 58
已采纳

如何在php中使用file_get_contents访问电子邮件帐户?

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.

  • 写回答

1条回答 默认 最新

  • douchao0358 2014-06-19 18:41
    关注

    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."
    ";
            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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教