您的位置:首页 > 编程语言 > PHP开发

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

2010-10-07 13:00 351 查看
<?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∂=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);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: