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

  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];
100         if(!$url){
101             return null;
102         }
103         $this->replace_url=$url;
104         //第二次跳转
105         $ch = curl_init($url);
106         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
107         curl_setopt($ch, CURLOPT_HEADER, true);
108         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
109         curl_setopt($ch, CURLOPT_POST, true);
110         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
111         //将之前保存的cookie信息,一起发送到服务器端
112         curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
113         curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
114         $result = curl_exec($ch);
115         curl_close($ch);
116         $fp=fopen("126_log.txt","w");
117         fwrite($fp,$result);
118         fclose($fp);
119         //取得sid
120         preg_match('/sid=[^\"].*/', $result, $location);
121         return substr($location[0], 4, -1);
122     }//end of function getSid
123     
124     function deleteMailById($mid){
125         if(!$this->is_valid){
126             return false;
127         }
128         if(!$this->sid){
129             return false;
130         }
131         
132         $URL="http://eg1a51.mail.126.com/a/s?"."sid=".$this->sid."&func=mbox:updateMessageInfos";
133         $xml_data = '<?xml version="1.0" ?><object><array name="ids">'.
134             '<string>'.$mid.'</string></array><object name="attrs"><int name="fid">4'.
135             '</int></object></object>';
136         //Set referer
137         $referer="http://eg1a51.mail.126.com/a/j/dm3/index.jsp?sid=".$sid;
138         //Set the header information
139         $header[]="Accept:text/javascript";
140         $header[]="Content-type:application/xml";
141         $ch = curl_init($URL);
142         curl_setopt($ch, CURLOPT_MUTE, 1);
143         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
144         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
145         curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie); //当前使用的cookie
146         curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie); //服务器返回的新cookie
147         curl_setopt($ch, CURLOPT_POST, 1);
148         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
149         curl_setopt($ch, CURLOPT_REFERER, $referer);
150         curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
151         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
152         $output = curl_exec($ch);
153         curl_close($ch);
154         if(strpos($output,"S_OK",1> 0){
155             return true;
156         }
157         return false;
158     }//end of delete Mai by Id
159 }
160 ?>

 

posted on 2010-05-07 16:38  Joshua Leung  阅读(2029)  评论(0编辑  收藏  举报

导航