$pop=newPop();echo $pop->connect("pop3.sina.net",110);echo $pop->user("shihan2@appdev.sinanet.com");echo $pop->pass("xxxx");echo $pop->stat();$pop->download($pop->lists());//smtp协议发邮件
$dir = dir(PATH);while($file = $dir->read()){if($file=="."|| $file==".."){continue;
}$smtp=newSmtp();echo $smtp->connect("smtp.sina.net",25);echo $smtp->helo("shihan2@appdev.sinanet.com");echo $smtp->auth();echo $smtp->user();echo $smtp->pass("xxxx");echo $smtp->mailFrom("shihan2@appdev.sinanet.com");echo $smtp->rcpt("shihan2@appdev.sinanet.com");echo $smtp->data();echo $smtp->send(file_get_contents(PATH.$file));
}
}catch (Exception $e) {echo $e->getMessage();
}classPop{private $socket;public function__construct(){ini_set('memory_limit', '200M');ini_set("auto_detect_line_endings", true);
}public function connect($popServer,$popPort){$res=@fsockopen("tcp://".$popServer,$popPort,$errno,$errstr);if(!$res){throw new Exception($errstr, $errno);
}$this->socket=$res;return $this->readLine();
}public function user($email){$user="USER {$email}\r\n";fwrite($this->socket,$user);return $this->readLine();
}public function pass($pwd){$pass="PASS {$pwd}\r\n";fwrite($this->socket,$pass);return $this->readLine();
}public functionlists(){fwrite($this->socket,"LIST\r\n");$lists=$this->read();return $this->parseList($lists);
}public function retr($id){fwrite($this->socket,"RETR {$id}\r\n");return $this->read();
}public function stat(){fwrite($this->socket,"STAT\r\n");return $this->readLine();
}public functionread() {$buf="";while ($ln = $this->readLine()) {if (trim($ln) == '.') {break;
}$buf .= $ln;
}return $buf;
}public function download($emails){foreach ($emails as $key => $email) {$name=$email['id'].".eml";if(!is_dir(PATH)){mkdir(PATH,0777);
}$path=PATH.$name;if(file_exists($path)){continue;
}echo "{$name} email is downloading... \r\n";$file=$this->retr($email['id']);file_put_contents($path, $file);echo "{$name} email is ok! \r\n";
}
}public functionreadLine(){$result="";while(true){$buffer=@fgets($this->socket,10);$n = strlen($buffer);$result.=$buffer;if (!$n) {break;
}if ($buffer[$n - 1] == "\n") {break;
}
}return $result;
}private function parseList($list){$result=array();$emails=explode("\n", $list);foreach ($emails as $key => $v) {$emailId=explode(" ", $v);if(!is_array($emailId)||$emailId[0]=='+OK'||!isset($emailId[0])||!isset($emailId[1])){continue;
}if($emailId[0][0]=='.'){break;
}$temp=array();$temp['id']=$emailId[0];$temp['size']=$emailId[1];$result[]=$temp;
}return $result;
}
}classSmtp{private $socket;private $email;public function__construct(){ini_set('memory_limit', '200M');ini_set("auto_detect_line_endings", true);
}public function connect($smtpServer,$smtpPort){$res=@fsockopen("tcp://".$smtpServer,$smtpPort,$errno, $errstr);if(!$res){throw new Exception($errstr, $errno);
}$this->socket=$res;return $this->readLine();
}public function helo($email){$user="HELO {$email}\r\n";fwrite($this->socket,$user);$this->email=$email;return $this->readLine();
}public functionauth(){$pass="AUTH LOGIN\r\n";fwrite($this->socket,$pass);return $this->readLine();
}public functionuser(){$pass=base64_encode($this->email)."\r\n";fwrite($this->socket,$pass);return $this->readLine();
}public function pass($pwd){$pass=base64_encode($pwd)."\r\n";fwrite($this->socket,$pass);return $this->readLine();
}public function mailFrom($from){$data="MAIL FROM:\r\n";fwrite($this->socket,$data);return $this->readLine();
}public function rcpt($rcpt){$data="RCPT TO:\r\n";fwrite($this->socket,$data);return $this->readLine();
}public functiondata(){$email="data\r\n";fwrite($this->socket,$email);return $this->readLine();
}public function send($data){$email="{$data}\r\n";$email.=".\r\n";fwrite($this->socket,$email);return $this->readLine();
}public functionread() {$buf="";while ($ln = $this->readLine()) {if (trim($ln) == '.') {break;
}$buf .= $ln;
}return $buf;
}public functionreadLine(){$result="";while(true){$buffer=@fgets($this->socket,10);$n = strlen($buffer);$result.=$buffer;if (!$n) {break;
}if ($buffer[$n - 1] == "\n") {break;
}
}return $result;
}
}