Home >php教程 >php手册 >PHP邮件发送类【完整版】

PHP邮件发送类【完整版】

WBOY
WBOYOriginal
2016-06-07 11:42:041010browse

支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件的邮件.
<?php <br /> /**<br> * 邮件发送类<br> * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件的邮件<br> * 需要的php扩展,sockets和Fileinfo。<br> * @example<br> * $mail = new MySendMail();<br> * $mail->setServer("XXXXX", "XXXXX@XXXXX", "XXXXX"); 设置smtp服务器<br> * $mail->setFrom("XXXXX"); 设置发件人<br> * $mail->setReceiver("XXXXX"); 设置收件人,多个收件人,调用多次<br> * $mail->setCc("XXXX"); 设置抄送,多个抄送,调用多次<br> * $mail->setBcc("XXXXX"); 设置秘密抄送,多个秘密抄送,调用多次<br> * $mail->setMailInfo("test", "<b>test</b>"); 设置邮件主题、内容<br> * $mail->sendMail(); 发送<br> */<br> class MySendMail {<br>     /**<br>     * @var string 邮件传输代理用户名<br>     * @access private<br>     */<br>     private $_userName;<br> <br>     /**<br>     * @var string 邮件传输代理密码<br>     * @access private<br>     */<br>     private $_password;<br> <br>     /**<br>     * @var string 邮件传输代理服务器地址<br>     * @access protected<br>     */<br>     protected $_sendServer;<br> <br>     /**<br>     * @var int 邮件传输代理服务器端口<br>     * @access protected<br>     */<br>     protected $_port=25;<br> <br>     /**<br>     * @var string 发件人<br>     * @access protected<br>     */<br>     protected $_from;<br> <br>     /**<br>     * @var string 收件人<br>     * @access protected<br>     */<br>     protected $_to;<br> <br>     /**<br>     * @var string 抄送<br>     * @access protected<br>     */<br>     protected $_cc;<br> <br>     /**<br>     * @var string 秘密抄送<br>     * @access protected<br>     */<br>     protected $_bcc;<br> <br>     /**<br>     * @var string 主题<br>     * @access protected<br>     */<br>     protected $_subject;<br> <br>     /**<br>     * @var string 邮件正文<br>     * @access protected<br>     */<br>     protected $_body;<br> <br>     /**<br>     * @var string 附件<br>     * @access protected<br>     */<br>     protected $_attachment;<br> <br>     /**<br>     * @var reource socket资源<br>     * @access protected<br>     */<br>     protected $_socket;<br> <br>     /**<br>     * @var string 错误信息<br>     * @access protected<br>     */<br>     protected $_errorMessage;<br> <br>     /**<br>     * 设置邮件传输代理,如果是可以匿名发送有邮件的服务器,只需传递代理服务器地址就行<br>     * @access public<br>     * @param string $server 代理服务器的ip或者域名<br>     * @param string $username 认证账号<br>     * @param string $password 认证密码<br>     * @param int $port 代理服务器的端口,smtp默认25号端口<br>     * @return boolean<br>     */<br>     public function setServer($server, $username="", $password="", $port=25) {<br>         $this->_sendServer = $server;<br>         $this->_port = $port;<br>         if(!empty($username)) {<br>             $this->_userName = base64_encode($username);<br>         }<br>         if(!empty($password)) {<br>             $this->_password = base64_encode($password);<br>         }<br>         return true;<br>     }<br> <br>     /**<br>     * 设置发件人<br>     * @access public<br>     * @param string $from 发件人地址<br>     * @return boolean<br>     */<br>     public function setFrom($from) {<br>         $this->_from = $from;<br>         return true;<br>     }<br> <br>     /**<br>     * 设置收件人,多个收件人,连续调用多次.<br>     * @access public<br>     * @param string $to 收件人地址<br>     * @return boolean<br>     */<br>     public function setReceiver($to) {<br>         if(isset($this->_to)) {<br>             if(is_string($this->_to)) {<br>                 $this->_to = array($this->_to);<br>                 $this->_to[] = $to;<br>                 return true;<br>             }<br>             elseif(is_array($this->_to)) {<br>                 $this->_to[] = $to;<br>                 return true;<br>             }<br>             else {<br>                 return false;<br>             }<br>         }<br>         else {<br>             $this->_to = $to;<br>             return true;<br>         }<br>     }<br> <br>     /**<br>     * 设置抄送,多个抄送,连续调用多次.<br>     * @access public<br>     * @param string $cc 抄送地址<br>     * @return boolean<br>     */<br>     public function setCc($cc) {<br>         if(isset($this->_cc)) {<br>             if(is_string($this->_cc)) {<br>                 $this->_cc = array($this->_cc);<br>                 $this->_cc[] = $cc;<br>                 return true;<br>             }<br>             elseif(is_array($this->_cc)) {<br>                 $this->_cc[] = $cc;<br>                 return true;<br>             }<br>             else {<br>                 return false;<br>             }<br>         }<br>         else {<br>             $this->_cc = $cc;<br>             return true;<br>         }<br>     }<br> <br>     /**<br>     * 设置秘密抄送,多个秘密抄送,连续调用多次<br>     * @access public<br>     * @param string $bcc 秘密抄送地址<br>     * @return boolean<br>     */<br>     public function setBcc($bcc) {<br>         if(isset($this->_bcc)) {<br>             if(is_string($this->_bcc)) {<br>                 $this->_bcc = array($this->_bcc);<br>                 $this->_bcc[] = $bcc;<br>                 return true;<br>             }<br>             elseif(is_array($this->_bcc)) {<br>                 $this->_bcc[] = $bcc;<br>                 return true;<br>             }<br>             else {<br>                 return false;<br>             }<br>         }<br>         else {<br>             $this->_bcc = $bcc;<br>             return true;<br>         }<br>     }<br> <br>     /**<br>     * 设置邮件信息<br>     * @access public<br>     * @param string $body 邮件主题<br>     * @param string $subject 邮件主体内容,可以是纯文本,也可是是HTML文本<br>     * @param string $attachment 附件,文件地址<br>     * @return boolean<br>     */<br>     public function setMailInfo($subject, $body, $attachment="") {<br>         $this->_subject = $subject;<br>         $this->_body = base64_encode($body);<br>         if(!empty($attachment)) {<br>             $this->_attachment = $attachment;<br>         }<br>         return true;<br>     }<br> <br>     /**<br>     * 发送邮件<br>     * @access public<br>     * @return boolean<br>     */<br>     public function sendMail() {<br>         $command = $this->getCommand();<br>         $this->socket();<br> <br>         foreach ($command as $value) {<br>             if($this->sendCommand($value[0], $value[1])) {<br>                 continue;<br>             }<br>             else{<br>                 return false;<br>             }<br>         }<br> <br>         //其实这里也没必要关闭,smtp命令:QUIT发出之后,服务器就关闭了连接,本地的socket资源会自动释放<br>         $this->close(); <br>         echo 'Mail OK!';<br>         return true;<br>     }<br> <br>     /**<br>     * 返回错误信息<br>     * @return string<br>     */<br>     public function error(){<br>         if(!isset($this->_errorMessage)) {<br>             $this->_errorMessage = "";<br>         }<br>         return $this->_errorMessage;<br>     }<br> <br>     /**<br>     * 返回mail命令<br>     * @access protected<br>     * @return array<br>     */<br>     protected function getCommand() {<br>         $command = array(<br>                 array("HELO sendmail\r\n", 250)<br>             );<br>         if(!empty($this->_userName)){<br>             $command[] = array("AUTH LOGIN\r\n", 334);<br>             $command[] = array($this->_userName . "\r\n", 334);<br>             $command[] = array($this->_password . "\r\n", 235);<br>         }<br>         $command[] = array("MAIL FROM:_from . ">\r\n", 250);<br> <br>         $separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符<br>         //设置发件人<br>         $header = "FROM: test_from . ">\r\n";<br> <br>         //设置收件人<br>         if(is_array($this->_to)) {<br>             $count = count($this->_to);<br>             for($i=0; $i                 $command[] = array("RCPT TO: _to[$i] . ">\r\n", 250);<br>                 if($i == 0){<br>                     $header .= "TO: _to[$i] .">";<br>                 }<br>                 elseif($i + 1 == $count){<br>                     $header .= ",_to[$i] .">\r\n";<br>                 }<br>                 else{<br>                     $header .= ",_to[$i] .">";<br>                 }<br>             }<br>         }<br>         else{<br>             $command[] = array("RCPT TO: _to . ">\r\n", 250);<br>             $header .= "TO: _to . ">\r\n";<br>         }<br> <br>         //设置抄送<br>         if(isset($this->_cc)) {<br>             if(is_array($this->_cc)) {<br>                 $count = count($this->_cc);<br>                 for($i=0; $i                     $command[] = array("RCPT TO: _cc[$i] . ">\r\n", 250);<br>                     if($i == 0){<br>                     $header .= "CC: _cc[$i] .">";<br>                     }<br>                     elseif($i + 1 == $count){<br>                         $header .= ",_cc[$i] .">\r\n";<br>                     }<br>                     else{<br>                         $header .= ",_cc[$i] .">";<br>                     }<br>                 }<br>             }<br>             else{<br>                 $command[] = array("RCPT TO: _cc . ">\r\n", 250);<br>                 $header .= "CC: _cc . ">\r\n";<br>             }<br>         }<br> <br>         //设置秘密抄送<br>         if(isset($this->_bcc)) {<br>             if(is_array($this->_bcc)) {<br>                 $count = count($this->_bcc);<br>                 for($i=0; $i                     $command[] = array("RCPT TO: _bcc[$i] . ">\r\n", 250);<br>                     if($i == 0){<br>                     $header .= "BCC: _bcc[$i] .">";<br>                     }<br>                     elseif($i + 1 == $count){<br>                         $header .= ",_bcc[$i] .">\r\n";<br>                     }<br>                     else{<br>                         $header .= ",_bcc[$i] .">";<br>                     }<br>                 }<br>             }<br>             else{<br>                 $command[] = array("RCPT TO: _bcc . ">\r\n", 250);<br>                 $header .= "BCC: _bcc . ">\r\n";<br>             }<br>         }<br> <br>         $header .= "Subject: " . $this->_subject ."\r\n";<br>         if(isset($this->_attachment)) {<br>             //含有附件的邮件头需要声明成这个<br>             $header .= "Content-Type: multipart/mixed;\r\n";<br>         }<br>         elseif(false){<br>             //邮件体含有图片资源的需要声明成这个<br>             $header .= "Content-Type: multipart/related;\r\n";<br>         }<br>         else{<br>             //html或者纯文本的邮件声明成这个<br>             $header .= "Content-Type: multipart/alternative;\r\n";<br>         }<br> <br>         //邮件头分隔符<br>         $header .= "\t" . 'boundary="' . $separator . '"';<br>         $header .= "\r\nMIME-Version: 1.0\r\n";<br>         $header .= "\r\n--" . $separator . "\r\n";<br>         $header .= "Content-Type:text/html; charset=utf-8\r\n";<br>         $header .= "Content-Transfer-Encoding: base64\r\n\r\n";<br>         $header .= $this->_body . "\r\n";<br>         $header .= "--" . $separator . "\r\n";<br> <br>         //加入附件<br>         if(isset($this->_attachment)){<br>             $header .= "\r\n--" . $separator . "\r\n";<br>             $header .= "Content-Type: " . $this->getMIMEType() . '; name="' . basename($this->_attachment) . '"' . "\r\n";<br>             $header .= "Content-Transfer-Encoding: base64\r\n";<br>             $header .= 'Content-Disposition: attachment; filename="' . basename($this->_attachment) . '"' . "\r\n";<br>             $header .= "\r\n";<br>             $header .= $this->readFile();<br>             $header .= "\r\n--" . $separator . "\r\n";<br>         }<br> <br>         $header .= "\r\n.\r\n";<br> <br>         $command[] = array("DATA\r\n", 354);<br>         $command[] = array($header, 250);<br>         $command[] = array("QUIT\r\n", 221);<br> <br>         return $command;<br>     }<br> <br>     /**<br>     * 发送命令<br>     * @access protected<br>     * @param string $command 发送到服务器的smtp命令<br>     * @param int $code 期望服务器返回的响应吗<br>     * @return boolean<br>     */<br>     protected function sendCommand($command, $code) {<br>         echo 'Send command:' . $command . ',expected code:' . $code . '<br>';<br>         //发送命令给服务器<br>         try{<br>             if(socket_write($this->_socket, $command, strlen($command))){<br> <br>                 //当邮件内容分多次发送时,没有$code,服务器没有返回<br>                 if(empty($code))  {<br>                     return true;<br>                 }<br> <br>                 //读取服务器返回<br>                 $data = trim(socket_read($this->_socket, 1024));<br>                 echo 'response:' . $data . '<br><br>';<br> <br>                 if($data) {<br>                     $pattern = "/^".$code."/";<br>                     if(preg_match($pattern, $data)) {<br>                         return true;<br>                     }<br>                     else{<br>                         $this->_errorMessage = "Error:" . $data . "|**| command:";<br>                         return false;<br>                     }<br>                 }<br>                 else{<br>                     $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());<br>                     return false;<br>                 }<br>             }<br>             else{<br>                 $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());<br>                 return false;<br>             }<br>         }catch(Exception $e) {<br>             $this->_errorMessage = "Error:" . $e->getMessage();<br>         }<br>     }<br> <br>     /**<br>     * 读取附件文件内容,返回base64编码后的文件内容<br>     * @access protected<br>     * @return mixed<br>     */<br>     protected function readFile() {<br>         if(isset($this->_attachment) && file_exists($this->_attachment)) {<br>             $file = file_get_contents($this->_attachment);<br>             return base64_encode($file);<br>         }<br>         else {<br>             return false;<br>         }<br>     }<br> <br>     /**<br>     * 获取附件MIME类型<br>     * @access protected<br>     * @return mixed<br>     */<br>     protected function getMIMEType() {<br>         if(isset($this->_attachment) && file_exists($this->_attachment)) {<br>             $mime = mime_content_type($this->_attachment);<br>             if(! preg_match("/gif|jpg|png|jpeg/", $mime)){<br>                 $mime = "application/octet-stream";<br>             }<br>             return $mime;<br>         }<br>         else {<br>             return false;<br>         }<br>     }<br> <br>     /**<br>     * 建立到服务器的网络连接<br>     * @access private<br>     * @return boolean<br>     */<br>     private function socket() {<br>         if(!function_exists("socket_create")) {<br>             $this->_errorMessage = "Extension sockets must be enabled";<br>             return false;<br>         }<br>         //创建socket资源<br>         $this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));<br> <br>         if(!$this->_socket) {<br>             $this->_errorMessage = socket_strerror(socket_last_error());<br>             return false;<br>         }<br> <br>         socket_set_block($this->_socket);//设置阻塞模式<br> <br>         //连接服务器<br>         if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {<br>             $this->_errorMessage = socket_strerror(socket_last_error());<br>             return false;<br>         }<br>         socket_read($this->_socket, 1024);<br> <br>         return true;<br>     }<br> <br>     /**<br>     * 关闭socket<br>     * @access private<br>     * @return boolean<br>     */<br>     private function close() {<br>         if(isset($this->_socket) && is_object($this->_socket)) {<br>             $this->_socket->close();<br>             return true;<br>         }<br>         $this->_errorMessage = "No resource can to be close";<br>         return false;<br>     }<br> }<br> <br> /**************************** Test ***********************************/<br> $mail = new MySendMail();<br> $mail->setServer("XXXXX", "XXXXX@XXXXX", "XXXXX");<br> $mail->setFrom("XXXXX@XXXXX");<br> $mail->setReceiver("XXXXX@XXXXX");<br> //$mail->setReceiver("XXXXX@XXXXX");<br> $mail->setCc("XXXXX@XXXXX");<br> $mail->setCc("XXXXX@XXXXX");<br> $mail->setBcc("XXXXX@XXXXX");<br> $mail->setBcc("XXXXX@XXXXX");<br> $mail->setBcc("XXXXX@XXXXX");<br> $mail->setMailInfo("test", "<b>test</b>", "sms.zip");<br> $mail->sendMail();

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn