首頁 >php教程 >php手册 >发一个可以同时兼容新浪云和百度云的邮件发送类

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

WBOY
WBOY原創
2016-06-07 11:44:051054瀏覽

目前只能发送文字信息,大家可以进行改进,欢迎将改进代码分享出来
感谢redspear 帮助改进,将新的代码放出了<?php <br /> class Email{<br>     public $config = array(<br>         'host'   => null,<br>         'port'   => 25,<br>         'user'   => null,<br>         'pass'   => null,<br>         'from'   => null,<br>         'debug'  => false,<br>         'isHtml' => false,<br>         'param'  => array('socket_create', 'fsockopen'),<br>         'method' => null,<br>     );<br> <br>     public function __set($key, $value){<br>         $this->config[$key] = $value;<br>     }<br> <br>     public function __get($key){<br>         return $this->config[$key];<br>     }<br>     <br>     public function __construct($config = array()){<br>         if(is_array($config)) $this->config = array_merge($this->config, $config);<br> <br>         if(is_null($this->host)) $this->host = C('EMAIL_SMTP');<br>         if(is_null($this->port)) $this->port = C('EMAIL_PORT');<br>         if(is_null($this->user)) $this->user = C('EMAIL_USER');<br>         if(is_null($this->pass)) $this->pass = C('EMAIL_PWD');<br>         if(is_null($this->from)) $this->from = C('EMAIL_FROM');<br> <br>         foreach($this->param as $index=>$method){<br>             if(function_exists($method)){<br>                 $this->method = $index;<br>                 $this->record("检测函数 {$method} 通过");<br>                 break;<br>             }<br>         }<br> <br>         $this->host = gethostbyname($this->host);<br>         $this->user = base64_encode($this->user);<br>         $this->pass = base64_encode($this->pass);<br> <br>         if(is_null($this->method)) $this->record('当前环境不支持发送邮件', true);<br>     }<br> <br>     //发送方法<br>     public function send($to='', $subject='', $body=''){<br>         if(is_null($this->method)){<br>             $this->result = false;<br>             return false;<br>         }<br> <br>         if(!$to || !$subject || !$body){<br>             $this->record('收信人信息不全', true);<br>             $this->result = false;<br>             return false;<br>         }<br> <br>         $this->to      = $to;      //收信人<br>         $this->subject = $subject; //邮件主题<br>         $this->body    = $body;    //邮件内容<br> <br>         $method = $this->param[$this->method];<br>         if(!method_exists($this, $method)){<br>             $this->record("调用方法 {$method} 不存在", true);<br>             $this->result = false;<br>             return false;<br>         }<br> <br>         $this->$method();<br>     }<br> <br>     private function socket_create(){<br>         $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);<br>         if($this->socket){<br>             $this->record('创建SOCKET:' . socket_strerror(socket_last_error()));<br>         }else{<br>             $this->record('初始化失败,请检查您的网络连接和参数', true);<br>             $this->result = false;<br>             return false;<br>         }<br>         $conn = socket_connect($this->socket, $this->host, $this->port);<br>         if($conn){<br>             $this->record('创建SOCKET连接:' . socket_strerror(socket_last_error()));<br>         }else{<br>             $this->record('初始化失败,请检查您的网络连接和参数', true);<br>             $this->result = false;<br>             return false;<br>         }<br>         $this->record("服务器应答:<font>".socket_read($this->socket, 1024)."</font>");<br> <br>         $this->handle();<br>     }<br> <br>     private function socket_create_call($params){<br>         socket_write($this->socket, $params[0], strlen($params[0]));<br>         $this->record("客户机命令:{$params[0]}");<br>         $msg = socket_read($this->socket, 1024);<br>         $this->record("服务器应答:<font>{$msg}</font>");<br> <br>         if(isset($params[1]) && strpos($msg, $params[1]) === false){<br>             $this->record($params[2], true);<br>             $this->result = false;<br>         }<br>     }<br> <br>     // fsockopen函数发送<br>     private function fsockopen(){<br>         $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, 60);<br>         if($this->socket){<br>             $this->record("创建SOCKET连接:".$this->host.':'.$this->port);<br>         }else{<br>             $this->record('初始化失败,请检查您的网络连接和参数'.$errstr, true);<br>             $this->result = false;<br>             return false;<br>         }<br>         stream_set_blocking($this->socket, true);<br>         $this->handle();<br>     }<br> <br>     private function fsockopen_call($params){<br>         fputs($this->socket, $params[0]);<br>         $this->record("客户机命令:{$params[0]}");<br>         $msg = fgets($this->socket, 512);<br>         $this->record("服务器应答:<font>{$msg}</font>");<br> <br>         if(isset($params[1]) && strpos($msg, $params[1]) === false){<br>             $this->record($params[2], true);<br>             $this->result = false;<br>         }<br>     }<br> <br>     private function handle(){<br>         $all = array();<br>         array_push($all, "From:{$this->from}\r\n");<br>         array_push($all, "To:{$this->to}\r\n");<br>         array_push($all, "Subject:=?utf-8?B?" . base64_encode($this->subject) . "?=\r\n");<br>         $this->isHtml ? array_push($all, "Content-Type: text/html;\r\n") : array_push($all, "Content-Type: text/plain;\r\n");  //邮件类型 html或文本<br>         array_push($all, "charset: utf-8\r\n");<br>         //告诉浏览器信件内容进过了base64编码,最后必须要发一组\r\n产生一个空行,表示头部信息发送完毕<br>         array_push($all, "Content-Transfer-Encoding: base64\r\n\r\n");<br>         array_push($all, base64_encode($this->body));<br> <br>         $all = implode('', $all);<br> <br>         $method = $this->param[$this->method];<br>         $call   = $method . '_call';<br> <br>         if(!method_exists($this, $call)){<br>             $this->record("调用方法 {$call} 不存在", true);<br>             $this->result = false;<br>             return false;<br>         }<br> <br>         $items = array(<br>             array("EHLO wangdong\r\n"),<br>             array("AUTH LOGIN\r\n"),<br>             array("{$this->user}\r\n"),<br>             array("{$this->pass}\r\n", '235', 'smtp 认证失败'),<br>             array("MAIL FROM:from}>\r\n", '250', '邮件发送失败'),<br>             array("RCPT TO:to}>\r\n", '250', '邮件发送失败'),<br>             array("DATA\r\n", '354', '邮件发送失败'),<br>             array("{$all}\r\n.\r\n", '250', '邮件发送失败'),<br>             array("QUIT\r\n"),<br>         );<br> <br>         //以下是和服务器会话<br>         foreach($items as $index=>$params){<br>             $this->$call($params);<br>             if($this->result === false) return false;<br> <br>             if($index == 0){<br>                 // fsockopen需要单独处理<br>                 while ($method == 'fsockopen') {<br>                     $msg = fgets($this->socket, 512);<br>                     $this->record("服务器应答:<font>{$msg}</font>");<br>                     if ((substr($msg, 3, 1) != '-') || empty($msg)) break;<br>                 }<br>             }<br>         }<br> <br>         if($this->result !== false) $this->result = true;<br>     }<br> <br>     //调试记录<br>     private function record($msg, $save = false){<br>         if($save) $this->error = $msg;<br>         if($this->debug) printf("<p>%s</p>\n", $msg);<br>     }<br> <br>     //关闭socket<br>     public function __destruct(){<br>         $method = $this->param[$this->method];<br>         switch ($method){<br>             case 'socket_create':<br>                 socket_close($this->socket);<br>                 break;<br>             case 'fsockopen':<br>                 fclose($this->socket);<br>                 break;<br>         }<br>     }<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn