<?php
/**
 * Created by PhpStorm.
 * User: eric
 * Date: 15/12/24
 * Time: 上午10:54
 */
class Mail{
    private $sock;
    private $errorStr;
    private $errorNo;
    private $timeout=30;
    private $host;
    private $port;
    private $user;
    private $password;
    function __construct($host,$port,$user,$pass,$format=1,$debug=0){
        $this->host = $host;
        $this->port = $port;
        $this->user = base64_encode($user);
        $this->password = base64_encode($pass);
        $this->sock = fsockopen($this->host,$this->port,$this->errorNo,$this->errorStr,$this->timeout);
        if(!$this->sock){
            exit("oops");
        }
        $response  = fgets($this->sock);
        if(strstr($response,'220') === false){
            exit("server error");
        }
    }
    function showDebug($response,$commond){
        echo $commond;
        echo $response.PHP_EOL;
    }
    function doCommond($commond,$return_code){
        fwrite($this->sock,$commond);
        $response = fgets($this->sock);
        if(stristr($response,"$return_code") == false){
            $this->showDebug($response,$commond);
            return false;
        }
        return true;
    }
    function sendMail($from,$to,$sub,$body){
        $detail = "FROM:".$from."\r\n";
        $detail.="To:".$to."\r\n";
        $detail.="Subject:".$sub."\r\n";
        $detail.="Content-type:text/plain;\r\n\r\n";
        $detail.=$body;
        $this->doCommond("HELO smtp.qq.com\r\n",250);
        $this->doCommond("AUTH LOGIN\r\n",334);
        $this->doCommond($this->user."\r\n",334);
        $this->doCommond($this->password."\r\n",235);
        $this->doCommond("MAIL FROM:".$from."\r\n",250);
        $this->doCommond("RCPT TO:".$to."\r\n",250);
        $this->doCommond("DATA\r\n",354);
        $this->doCommond($detail."\r\n.\r\n",250);
        $this->doCommond("QUIT\r\n",221);
        return true;
    }
}
$mail  =  new Mail('ssl://smtp.qq.com',465,'xxx@qq.com','特殊的密码,因为我有邮箱独立密码,所以只能使用qq邮箱额外提供的');
$mail->sendMail('xxx@qq.com','xxxx@qq.com','hi','嘿嘿嘿');