<?php
/****
自学it网 高端PHP培训
论 坛: http://www.zixue.it
微 博: http://weibo.com/Yshiba
****/
/**
用户往[email protected]发送一封邮件,内容随意
系统就可以把此用户激活
1: 用PHP+POP3协议收取信件(PHP+SOCKET编程)
2: 收到信件,分析发件人,并激活该用户
3: 每隔几分钟,自动运行一次(linux下用crontab做定时任务)
**/
class pop3 {
const CRLF = "\r\n";
protected $host = 'pop3.163.com';
protected $port = 110;
protected $errno = -1;
protected $errstr = '';
protected $user = 'php0620';
protected $pass = 'pwd';
protected $fh = NULL; // 放置连接资源
// 连接服务器
public function conn() {
$this->fh = fsockopen($this->host,$this->port,$this->errno,$this->errstr,3);
}
public function login() {
fwrite($this->fh,'user ' . $this->user . self::CRLF);
if(substr($this->getLine(),0,3) != '+OK') {
throw new Exception("用户名不正确");
}
fwrite($this->fh,'pass ' . $this->pass . self::CRLF);
if(substr($this->getLine(),0,3) != '+OK') {
throw new Exception("密码不正确");
}
}
/*
// 查询一共有多少邮件,便于循环取每一封邮件
public function getCnt() {
fwrite($this->fh,'stat' . ' ' . self::CRLF);
$tmp = explode(' ',$this->getLine());
return $tmp[1];
}*/
// 查询出所有的邮件发信人
public function getAll() {
fwrite($this->fh,'top 1 1' . self::CRLF);
$post = array();
while( stripos(($row = fgets($this->fh)),'from:') === false) {
}
$post[] = $row;
return $post;
}
protected function getLine() {
return fgets($this->fh);
}
// 发送用户名+密码 登陆
// 获取邮件
}
$pop = new pop3();
try {
$pop->conn();
$pop->login();
echo '发信人是';print_r($pop->getAll());
} catch(exception $e) {
echo $e->getMessage();
}
// 获取收集人,之后
//连接mysql,根据email,激活该用户