php收取邮件

有的网站用这种方式来激活用户。让你用注册邮箱给指定的邮箱发信;如土豆;

/*让用户往公司的企业邮箱发信,收到后给他激活
思路:
用php+POP协议收取信件(php+socket编程)
收到信件,分析发件人,并激活该用户
让系统每个几分钟运行一次,用linux的crontab做定时任务
*/

telnet 收取信件

ctrl +】 回车

输入user pass

top 1 5 是第一封邮件的前5行的意思

如果要提取发件人信息,只取得1行信息就够了,top 1 1

<?php
/****
自学it网 高端PHP培训

论  坛: http://www.zixue.it
微  博: http://weibo.com/Yshiba
****/


/**
用户往php0620@163.com发送一封邮件,内容随意
系统就可以把此用户激活

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 = 'xxxxxxxxx';
    protected $pass = '*******';

    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,激活该用户

 

posted @ 2014-11-24 17:26  王孙将归  阅读(284)  评论(0编辑  收藏  举报