利用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
}
?>
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
}
?>
相关文章推荐
- andriod———1.实现原生登录注册。 2.首页完成商品列表展示点击相对应的商品ID进入商品详情 3.商品详情页面展示数据,点击加入购物车进入购物车页面 4.购物车页面完成购物车编辑,删除,功能
- hibernateTemplate的根据id来删除数据
- oracle利用行号创建id根据id分页
- Oracle 删除重复数据只留一条 查询及删除重复记录的SQL语句 1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 wher
- Oracle 删除重复数据只留一条 查询及删除重复记录的SQL语句 1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 wher
- 删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最大的记录
- android 根据控件的ID字符串利用反射获取UI控件
- php实例根据ID删除mysql表中的数据
- 数据库修改之更新员工表,根据ID删除Mapping表数据,再插入数据
- 根据父级ID生成一个数据库不重复的新ID,(补漏,开头删除,中间删除,的ID都可以按顺序补上)
- 利用ssh-copy-id无需密码登录远程服务器
- 在做mes系统时,利用D7 IdMessage&IdSMTP发送邮件,从盒子上学习了不少的东西,自己整理了一下,想share给新手,学习参考!
- 数据库删除之根据ID删除父表中的数据和根据ID删除子表数据
- 根据id删除数据,重新绑定数据源
- mongodb 根据主键id 删除数据
- jQuery根据ID删除元素
- 不显示删除回复显示所有回复显示星级回复显示得分回复 [推荐] php 模拟GMAIL,HOTMAIL(MSN),YAHOO,163,126邮箱登录
- jquery根据id删除元素
- 利用ssh-copy-id无需密码登录远程服务器
- sqlserver 根据一个泛型id查询数据,批量删除数据