您好,欢迎访问一九零五行业门户网

发一个可以同时兼容新浪云和百度云的邮件发送类

目前只能发送文字信息,大家可以进行改进,欢迎将改进代码分享出来
感谢redspear 帮助改进,将新的代码放出了 null,
        'port'   => 25,
        'user'   => null,
        'pass'   => null,
        'from'   => null,
        'debug'  => false,
        'ishtml' => false,
        'param'  => array('socket_create', 'fsockopen'),
        'method' => null,
    );
public function __set($key, $value){
        $this->config[$key] = $value;
    }
public function __get($key){
        return $this->config[$key];
    }
public function __construct($config = array()){
        if(is_array($config)) $this->config = array_merge($this->config, $config);
if(is_null($this->host)) $this->host = c('email_smtp');
        if(is_null($this->port)) $this->port = c('email_port');
        if(is_null($this->user)) $this->user = c('email_user');
        if(is_null($this->pass)) $this->pass = c('email_pwd');
        if(is_null($this->from)) $this->from = c('email_from');
foreach($this->param as $index=>$method){
            if(function_exists($method)){
                $this->method = $index;
                $this->record(检测函数 {$method} 通过);
                break;
            }
        }
$this->host = gethostbyname($this->host);
        $this->user = base64_encode($this->user);
        $this->pass = base64_encode($this->pass);
if(is_null($this->method)) $this->record('当前环境不支持发送邮件', true);
    }
//发送方法
    public function send($to='', $subject='', $body=''){
        if(is_null($this->method)){
            $this->result = false;
            return false;
        }
if(!$to || !$subject || !$body){
            $this->record('收信人信息不全', true);
            $this->result = false;
            return false;
        }
$this->to      = $to;      //收信人
        $this->subject = $subject; //邮件主题
        $this->body    = $body;    //邮件内容
$method = $this->param[$this->method];
        if(!method_exists($this, $method)){
            $this->record(调用方法 {$method} 不存在, true);
            $this->result = false;
            return false;
        }
$this->$method();
    }
private function socket_create(){
        $this->socket = socket_create(af_inet, sock_stream, sol_tcp);
        if($this->socket){
            $this->record('创建socket:' . socket_strerror(socket_last_error()));
        }else{
            $this->record('初始化失败,请检查您的网络连接和参数', true);
            $this->result = false;
            return false;
        }
        $conn = socket_connect($this->socket, $this->host, $this->port);
        if($conn){
            $this->record('创建socket连接:' . socket_strerror(socket_last_error()));
        }else{
            $this->record('初始化失败,请检查您的网络连接和参数', true);
            $this->result = false;
            return false;
        }
        $this->record(服务器应答:.socket_read($this->socket, 1024).);
$this->handle();
    }
private function socket_create_call($params){
        socket_write($this->socket, $params[0], strlen($params[0]));
        $this->record(客户机命令:{$params[0]});
        $msg = socket_read($this->socket, 1024);
        $this->record(服务器应答:{$msg});
if(isset($params[1]) && strpos($msg, $params[1]) === false){
            $this->record($params[2], true);
            $this->result = false;
        }
    }
// fsockopen函数发送
    private function fsockopen(){
        $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, 60);
        if($this->socket){
            $this->record(创建socket连接:.$this->host.':'.$this->port);
        }else{
            $this->record('初始化失败,请检查您的网络连接和参数'.$errstr, true);
            $this->result = false;
            return false;
        }
        stream_set_blocking($this->socket, true);
        $this->handle();
    }
private function fsockopen_call($params){
        fputs($this->socket, $params[0]);
        $this->record(客户机命令:{$params[0]});
        $msg = fgets($this->socket, 512);
        $this->record(服务器应答:{$msg});
if(isset($params[1]) && strpos($msg, $params[1]) === false){
            $this->record($params[2], true);
            $this->result = false;
        }
    }
private function handle(){
        $all = array();
        array_push($all, from:{$this->from}\r\n);
        array_push($all, to:{$this->to}\r\n);
        array_push($all, subject:=?utf-8?b? . base64_encode($this->subject) . ?=\r\n);
        $this->ishtml ? array_push($all, content-type: text/html;\r\n) : array_push($all, content-type: text/plain;\r\n);  //邮件类型 html或文本
        array_push($all, charset: utf-8\r\n);
        //告诉浏览器信件内容进过了base64编码,最后必须要发一组\r\n产生一个空行,表示头部信息发送完毕
        array_push($all, content-transfer-encoding: base64\r\n\r\n);
        array_push($all, base64_encode($this->body));
$all = implode('', $all);
$method = $this->param[$this->method];
        $call   = $method . '_call';
if(!method_exists($this, $call)){
            $this->record(调用方法 {$call} 不存在, true);
            $this->result = false;
            return false;
        }
$items = array(
            array(ehlo wangdong\r\n),
            array(auth login\r\n),
            array({$this->user}\r\n),
            array({$this->pass}\r\n, '235', 'smtp 认证失败'),
            array(mail from:from}>\r\n, '250', '邮件发送失败'),
            array(rcpt to:to}>\r\n, '250', '邮件发送失败'),
            array(data\r\n, '354', '邮件发送失败'),
            array({$all}\r\n.\r\n, '250', '邮件发送失败'),
            array(quit\r\n),
        );
//以下是和服务器会话
        foreach($items as $index=>$params){
            $this->$call($params);
            if($this->result === false) return false;
if($index == 0){
                // fsockopen需要单独处理
                while ($method == 'fsockopen') {
                    $msg = fgets($this->socket, 512);
                    $this->record(服务器应答:{$msg});
                    if ((substr($msg, 3, 1) != '-') || empty($msg)) break;
                }
            }
        }
if($this->result !== false) $this->result = true;
    }
//调试记录
    private function record($msg, $save = false){
        if($save) $this->error = $msg;
        if($this->debug) printf(%s
\n, $msg);
    }
//关闭socket
    public function __destruct(){
        $method = $this->param[$this->method];
        switch ($method){
            case 'socket_create':
                socket_close($this->socket);
                break;
            case 'fsockopen':
                fclose($this->socket);
                break;
        }
    }
}
ad:真正免费,域名+虚机+企业邮箱=0元
其它类似信息

推荐信息