设为首页 收藏本站
查看: 1081|回复: 0

[经验分享] [PHP]用PHP模拟126邮箱的登陆过程来收取邮件

[复制链接]

尚未签到

发表于 2015-8-27 09:18:15 | 显示全部楼层 |阅读模式
  126邮箱的pop3收信是收费服务,而且还不能收到垃圾邮件夹中的邮件,所以写了下面的小程序来收信。初学者,大家多多指教。
  在写下面的代码过程中最吐血的事情是:在完成度到达 90% 的时候,不小心把源文件删除了。  由于是在linux下写的,在权衡了找回文件和重写之间,我还是选择了后者……555。
  
  不多说了,粘代码:
  

DSC0000.gif DSC0001.gif Code
<?php
/*
* 收取126邮箱中的信件
* @filename 126_curl.php
* @touch date Tue 28 Apr 2009 20:49:12 AM CST
* @package Get_Ganji_test_mails
* @author zhangyufeng
* @version 1.0.0
* @copyright (c) 2009, zhangyufeng@staff.ganji.com
*/
error_reporting(0);
$user       = '****';
$pass       = '*****';
$mail_addr  = '****@126.com';
$save_file  = 'result.126.com.' . strtotime('now');
//{{{  格式化邮件内容
function read_126_mail($mail_contents)
{
   try{
            $xml = new SimpleXMLElement($mail_contents);
            $mail_all    = array();
            $mail_detail = array();
            foreach($xml->array->object as $item)
            {
                $arrays = (array)$item->string[0];
                $mail_detail['id']           = array_pop($arrays);
                $arrays = (array)$item->string[1];
                $mail_detail['to']           = array_pop($arrays);
               
                $arrays = (array)$item->int[0];
                $mail_detail['fid']          = array_pop($arrays);
                $arrays = (array)$item->string[1];
                $mail_detail['from']         = array_pop($arrays);
                $arrays = (array)$item->string[3];
                $mail_detail['subject']      = array_pop($arrays);
                $arrays = (array)$item->date[0];
                $mail_detail['sentDate']     = array_pop($arrays);
                $arrays = (array)$item->date[1];
                $mail_detail['receivedDate'] = array_pop($arrays);
               array_push($mail_all, $mail_detail);
            }
            return $mail_all;
   }catch(Exception $e)
    {
        print_r($e->getMessage());
        exit();
    }
}
//}}}

/*=======================================================================================
  开始登录 ganjizyf@126.com
  =======================================================================================
*/

$url            = 'http://reg.163.com/login.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26language%3D0%26style%3D-1';
$ch             = curl_init($url);
$cookie         = tempnam('.', 'cookie');
$referer_login  = 'http://mail.126.com';
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_REFERER, $referer_login);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$fields_post = array(
        'domain'    => '126.com' ,
        'language'  => 0 ,
        'bCookie'   => '' ,
        'username'  => $mail_addr ,
        'savelogin' => '',
        'user'      => $user ,
        'remUser'   => '',
        'secure'    => '',
        'password'  => $pass
);
$headers_login = array(
    'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0',
    'Referer'           => 'http://www.126.com'
);
$fields_string = '';
foreach($fields_post as $key => $value)
{
    $fields_string .= $key . '=' . $value . '&';
}
$fields_string = rtrim($fields_string , '&');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$result_login = curl_exec($ch);
curl_close($ch);

/*=======================================================================================
  开始第一次跳转
  =======================================================================================
*/
//取得跳转地址
preg_match('/http:\/\/passport.126.com.*loginyoudao=0/', $result_login, $url_c_1);
$ch = curl_init($url_c_1[0]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
$referer_check_1 = $url;
curl_setopt($ch, CURLOPT_REFERER, $referer_check_1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$result_check_1 = curl_exec($ch);
curl_close($ch);
//var_dump($result_check_1);

/*=======================================================================================
  开始第二次跳转
  =======================================================================================
*/

//取得跳转地址
preg_match('/http:\/\/entry.mail.126.com.*ganjizyf@126.com/', $result_check_1, $url_c_2);
$ch = curl_init($url_c_2[0]);
$headers_check_2 = array(
    'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0'
);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_check_2);
$referer_check_2 = $url_c_1[0];
curl_setopt($ch, CURLOPT_REFERER, $referer_check_2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$result_check_2 = curl_exec($ch);
curl_close($ch);
//取得sid
preg_match('/sid=[^\"].*/', $result_check_2, $location);
$sid = substr($location[0], 4, -1);
/*=======================================================================================
   开始第三次跳转
  =======================================================================================
*/
//取得跳转地址
preg_match('/http:.*/', $result_check_2, $url_c_3);
$ch = curl_init($url_c_3[0]);
$headers_check_2 = array(
    'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0',
    'Referer'           => 'http://www.126.com'
);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
$referer_check_3 = $url_c_2[0];
curl_setopt($ch, CURLOPT_REFERER, $referer_check_3);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$result_check_3 = curl_exec($ch);
curl_close($ch);

/*=======================================================================================
  开始收取邮件
  =======================================================================================
*/
//取得收信地址
preg_match('/http:.*/', $result_check_2, $url_c_3);
$url = "http://tg1b12.mail.126.com/a/s?sid=${sid}&func=mbox:listMessages";
$ch = curl_init($url);
$headers_get_mail = array(
    'Host'              => 'tg1b12.mail.126.com',
    'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0',
    'Acccept'           => 'text/javascript',
    'Referer'           => 'http://tg1b12.mail.126.com/a/j/dm3/index.jsp?sid=' . $sid
);
$fields_string = '<?xml version="1.0"?><object><int name="fid">1</int><string name="order">date</string><boolean name="desc">true</boolean><int name="start">0</int><int name="limit">20</int></object>';
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
$referer_get = 'http://tg1b12.mail.126.com/a/j/dm3/index.jsp?sid='. $sid;
curl_setopt($ch, CURLOPT_REFERER, $referer_get);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$result_mails_list = curl_exec($ch);
curl_close($ch);

$array_mails = read_126_mail($result_mails_list);
if(is_array($array_mails))
{   
    foreach($array_mails as $key => $value)
    {
                /* 获取邮件全部HTML代码(显示时可以直接使用)
                //{{{
                //开始得到ssid
                $url = "http://tg1b12.mail.126.com/jy3/read/read.jsp?offset=0&mid={$array_mails[$key]['id']}&sid=${sid}&fid={$array_mails[$key]['fid']}";
                $array_mails[$key]['url_1'] = $url;
                $ch = curl_init($url);
                $headers_get_mail = array(
                    'Host'              => 'tg1b12.mail.126.com',
                    'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0',
                    'Acccept'           => 'text/javascript',
                    'Referer'           => 'http://tg1b12.mail.126.com/a/j/dm3/index.jsp?sid=' . $sid
                );
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
                curl_setopt($ch, CURLOPT_POST, false);
                $referer_get = 'http://tg1b12.mail.126.com/jy3/mbox/list.jsp?sid=${sid}&fid=1';
                curl_setopt($ch, CURLOPT_REFERER, $referer_get);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
                $result_ssid_string = curl_exec($ch);
                curl_close($ch);
               
                preg_match('/ssid=(.*%3d)&mid/', $result_ssid_string, $ssids);   
                $ssid = $ssids[1];
               
                //开始得到邮件正文(HTML)
                $url = "http://tg1b12.mail.126.com/jy3/read/viewMailHTML.jsp?ssid=${ssid}&mid={$array_mails[$key]['id']}&partId=0&isSearch=&filterScripts=true&filterStylesheets=false&filterObjects=true&filterFrames=true";
                $array_mails[$key]['url_2'] = $url;
                $ch = curl_init($url);
                $headers_get_mail = array(
                    'Host'              => 'tg1b12.mail.126.com',
                    'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0',
                    'Acccept'           => 'text/javascript',
                    'Referer'           => 'http://tg1b12.mail.126.com/a/j/dm3/index.jsp?sid=' . $sid
                );

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
                curl_setopt($ch, CURLOPT_POST, false);
                $referer_get = "http://tg1b12.mail.126.com/jy3/read/read.jsp?offset=0&mid={$array_mails[$key]['id']}&sid=${sid}&fid=1";
                curl_setopt($ch, CURLOPT_REFERER, $referer_get);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
                $result_mail_content = curl_exec($ch);
                curl_close($ch);
                */               
                //}}}
                //获得邮件原文

                $url = "http://tg1b12.mail.126.com/jy3/read/readdata.jsp?sid=${sid}&mid={$array_mails[$key]['id']}&mode=text&part=0";
                $array_mails[$key]['url_2'] = $url;
                $ch = curl_init($url);
                $headers_get_mail = array(
                    'Host'              => 'tg1b12.mail.126.com',
                    'User-Agent'        => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0',
                    'Acccept'           => 'text/javascript',
                    'Referer'           => 'http://tg1b12.mail.126.com/a/j/dm3/index.jsp?sid=' . $sid
                );

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
                curl_setopt($ch, CURLOPT_POST, false);
                $referer_get = "http://tg1b12.mail.126.com/jy3/read/read.jsp?offset=0&mid={$array_mails[$key]['id']}&sid=${sid}&fid=1";
                curl_setopt($ch, CURLOPT_REFERER, $referer_get);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
                curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
                $result_mail_content = curl_exec($ch);
                curl_close($ch);

                $array_mails[$key]['contents'] = $result_mail_content;
                if($array_mails[$key]['fid'] == '5')
                {
                     $array_mails[$key]['type'] = '垃圾邮件';
                }  
                else
                {
                     $array_mails[$key]['type'] = '正常邮件';
                }
//                var_dump($array_mails[$key]);
    }
}
else
{
    print_r('error!');
    exit();
}
file_put_contents($save_file, json_encode($array_mails));
unlink($cookie);
?>  
  最后,想问一下哪位大大知道收取Msn类邮件的方法,不胜感激!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-104805-1-1.html 上篇帖子: php中类能够调用实例方法不报错的原因 下篇帖子: PHP 3:从Login界面谈PHP标记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表