pangxia75 发表于 2015-8-27 09:18:15

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

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

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;
                $mail_detail['id']         = array_pop($arrays);
                $arrays = (array)$item->string;
                $mail_detail['to']         = array_pop($arrays);
               
                $arrays = (array)$item->int;
                $mail_detail['fid']          = array_pop($arrays);
                $arrays = (array)$item->string;
                $mail_detail['from']         = array_pop($arrays);
                $arrays = (array)$item->string;
                $mail_detail['subject']      = array_pop($arrays);
                $arrays = (array)$item->date;
                $mail_detail['sentDate']   = array_pop($arrays);
                $arrays = (array)$item->date;
                $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);
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);
$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;
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, 4, -1);
/*=======================================================================================
   开始第三次跳转
=======================================================================================
*/
//取得跳转地址
preg_match('/http:.*/', $result_check_2, $url_c_3);
$ch = curl_init($url_c_3);
$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;
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;
               
                //开始得到邮件正文(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]
查看完整版本: [PHP]用PHP模拟126邮箱的登陆过程来收取邮件