[Linux] PHP程序员玩转Linux系列-telnet轻松使用邮箱

1.PHP程序员玩转Linux系列-怎么安装使用CentOS

2.PHP程序员玩转Linux系列-lnmp环境的搭建

3.PHP程序员玩转Linux系列-搭建FTP代码开发环境

4.PHP程序员玩转Linux系列-备份还原MySQL

5.PHP程序员玩转Linux系列-自动备份与SVN

6.PHP程序员玩转Linux系列-Linux和Windows安装nginx

7.PHP程序员玩转Linux系列-nginx初学者引导

8.PHP程序员玩转Linux系列-Nginx中的HTTPS

9.PHP程序员玩转Linux系列-使用supervisor实现守护进程

10.PHP程序员玩转Linux系列-升级PHP到PHP7

 

邮箱是工作中非常重要的一个工具,平常我都是使用foxmail软件或者直接登录web来操作邮件,现在我要换种方式使用邮箱.使用邮箱都是通过pop协议收取邮件,使用smtp协议发送邮件,现在我就直接在命令行中来操作一下邮箱.

pop服务器非SSL加密,一般的端口是110,例如:pop3.sina.net  端口:110

telnet pop3.sina.net 110

使用USER指令,指定邮箱名

USER shihan2@appdev.sinanet.com

使用PASS指令,指定密码

PASS 密码xxx

使用STAT指令,查看邮箱统计,前一个是邮件数,后一个是邮件所占的空间大小

STAT

使用LIST指令,列出邮件列表,前一个是邮件的编号,后一个是该邮件所占的大小

LIST

 

使用RETR指令,读取邮件详情,RETR 编号,读出来的就是信体内容了

RETR 1

 

使用smtp发送邮件

使用如下命令

telnet smtp.sina.cn 25
ehlo sina.cn
auth login
xxxxxxxxxxxxxxxxxxxx== #base64加密的邮箱
MjAzOTQ0LmNvbQ==       #base64加密的密码
mail from:<shihan@appdev.sinanet.com> #发件人
rcpt to:<630892807@qq.com>  #收件人
data
To:630892807@qq.com
From:shihan@appdev.sinanet.com
Subject:测试一下telnet发邮件

测试一下telnet发邮件测试一下telnet发邮件

.  #这个必须有

PHP代码实现收发信

<?php
try {
    define('PATH', dirname(__FILE__).'/emails/');
    //pop协议读取下载邮件
    $pop=new Pop();
    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=new Smtp();
        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();
}
class Pop{
    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 function lists(){
        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 function read() {
        $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 function readLine(){
        $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;
    }
}
class Smtp{
    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 function auth(){
        $pass="AUTH LOGIN\r\n";
        fwrite($this->socket,$pass);
        return $this->readLine();
    }
    public function user(){
        $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:<{$from}>\r\n";
        fwrite($this->socket,$data);
        return $this->readLine();
    }
    public function rcpt($rcpt){
        $data="RCPT TO:<{$rcpt}>\r\n";
        fwrite($this->socket,$data);
        return $this->readLine();
    }
    public function data(){
        $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 function read() {
        $buf="";
        while ($ln = $this->readLine()) {
            if (trim($ln) == '.') {
                break;
            }
            $buf .= $ln;
        }
        return $buf;
    }
    public function readLine(){
        $result="";
        while(true){
            $buffer=@fgets($this->socket,10);
            $n = strlen($buffer);
            $result.=$buffer;
            if (!$n) {
                break;
            }
            if ($buffer[$n - 1] == "\n") {
                break;
            }
        }
        return $result;
    }

}

 

posted @ 2017-04-21 09:56  唯一客服系统开发笔记  阅读(840)  评论(0编辑  收藏  举报