您的位置:首页 > 其它

利用cURL登录126邮箱,并根据邮件ID来删除邮件

2010-05-07 16:38 369 查看
1 <?
2 $user="*****@126.com";
3 $pass="******";
4 $mail=new Mail126($user,$pass);
5 echo $mail->verifyLogin()==true?"Login Successfully":"Login Failed";
6 $mid="173:1tbirQ6+PEX9ejU9qQAAsC";
7 echo "<BR/>";
8 echo $mail->deleteMailById($mid)==true?"Delete successfully":"Delete Mail $mid Failed";
9
10 class Mail126{
11 private $user;
12 private $pass;
13 private $sid;
14 private $cookie;
15 private $is_valid;
16 private $replace_url;
17 public function Mail126($user,$pass){
18 $this->user=$user;
19 $this->pass=$pass;
20 $this->is_valid=false;
21 }
22 public function verifyLogin(){
23 //验证登录,顺便得到Sid
24 $url="https://reg.163.com/logins.jsp?type=1&product=mail126&url=http://entry.mail.126.com/cgi/ntesdoor?hid%3D10010102%26lightweight%3D1%26verifycookie%3D1%26language%3D0%26style%3D-1";
25 $ch=curl_init($url);
26 $this->cookie=tempnam("./tmp","xss126");
27 $referer_login="http://www.126.com";
28 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
29 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
30 curl_setopt($ch, CURLOPT_HEADER, true);
31 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
32 curl_setopt($ch, CURLOPT_POST, true);
33 curl_setopt($ch, CURLOPT_REFERER, $referer_login);
34
35 $fields_post = array(
36 'username'=> $this->user,
37 'password'=> $this->pass,
38 'verifycookie'=>1,
39 'style'=>-1,
40 'product'=> 'mail126',
41 'selType'=>-1,
42 'secure'=>'on'
43 );
44
45 $headers_login = array(
46 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0',
47 'Referer' => 'http://www.126.com'
48 );
49
50 $fields_string = '';
51
52 foreach($fields_post as $key => $value){
53 $fields_string .= $key . '=' . $value . '&';
54 }
55 $fields_string = rtrim($fields_string , '&');
56 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
57 //关闭连接时,将服务器端返回的cookie保存在以下文件中
58 curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
59 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_login);
60 curl_setopt($ch, CURLOPT_POST, count($fields));
61 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
62 $result= curl_exec($ch);
63 curl_close($ch);
64 preg_match("/replace\(\"(.*?)\"\)\;/", $result, $mtitle);
65 $url = $mtitle[1];
66 //echo "<p style='color:green;'>Redirect URL".$url."</p>";
67 if(!$url){
68 return false;
69 }else{
70 $this->is_valid=true;
71 $this->sid=$this->getSid($url);
72 return true;
73 }
74 }//end of function verifyLogin
75
76 public function getSid($replace_url){
77 if($this->is_valid == false){
78 return null;
79 }
80 if($this->sid){
81 return $this->sid;
82 }
83 $ch = curl_init($replace_url);
84 $headers = array(
85 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0'
86 );
87
88 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
89 curl_setopt($ch, CURLOPT_HEADER, true);
90 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
91 curl_setopt($ch, CURLOPT_POST, true);
92 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
93 //将之前保存的cookie信息,一起发送到服务器端
94 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
95 curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
96 $result = curl_exec($ch);
97 curl_close($ch);
98 preg_match("/replace\(\"(.*?)\"\)\;/", $result, $mtitle);
99 $url = $mtitle[1];
if(!$url){
return null;
}
$this->replace_url=$url;
//第二次跳转
$ch = curl_init($url);
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);
//将之前保存的cookie信息,一起发送到服务器端
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
$result = curl_exec($ch);
curl_close($ch);
$fp=fopen("126_log.txt","w");
fwrite($fp,$result);
fclose($fp);
//取得sid
preg_match('/sid=[^\"].*/', $result, $location);
return substr($location[0], 4, -1);
}//end of function getSid

function deleteMailById($mid){
if(!$this->is_valid){
return false;
}
if(!$this->sid){
return false;
}

$URL="http://eg1a51.mail.126.com/a/s?"."sid=".$this->sid."&func=mbox:updateMessageInfos";
$xml_data = '<?xml version="1.0" ?><object><array name="ids">'.
'<string>'.$mid.'</string></array><object name="attrs"><int name="fid">4'.
'</int></object></object>';
//Set referer
$referer="http://eg1a51.mail.126.com/a/j/dm3/index.jsp?sid=".$sid;
//Set the header information
$header[]="Accept:text/javascript";
$header[]="Content-type:application/xml";
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); //当前使用的cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); //服务器返回的新cookie
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
if(strpos($output,"S_OK",1) > 0){
return true;
}
return false;
}//end of delete Mai by Id
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐