[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

图片 1

行使USER指令,指定邮箱名

USER shihan2@appdev.sinanet.com

动用PASS指令,指定密码

PASS 密码xxx

应用STAT指令,查看邮箱总结,前一个是邮件数,后一个是邮件所占的长空尺寸

STAT

动用LIST指令,列出邮件列表,前一个是邮件的编号,后一个是该邮件所占的分寸

LIST

图片 2

 图片 3

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

RETR 1

图片 4

 

行使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;
    }

}

 

网站地图xml地图