class.phpmailer.php

来自「asterisk用 的voip记费软件」· PHP 代码 · 共 1,389 行 · 第 1/3 页

PHP
1,389 行
字号
        else        {            $header[] = sprintf("Content-Transfer-Encoding: %s\r\n", $this->Encoding);            $header[] = sprintf("Content-Type: %s; charset = \"%s\"",                                $this->ContentType, $this->CharSet);            // No additional lines when using mail() function            if($this->Mailer != "mail")                $header[] = "\r\n\r\n";        }        return(join("", $header));    }    /**     * Assembles the message body.  Returns a string if successful     * or false if unsuccessful.     * @private     * @returns string     */    function create_body() {        // wordwrap the message body if set        if($this->WordWrap)            $this->Body = $this->wordwrap($this->Body, $this->WordWrap);        // If content type is multipart/alternative set body like this:        if ((!empty($this->AltBody)) && (count($this->attachment) < 1))        {            // Return text of body            $mime = array();            $mime[] = "This is a MIME message. If you are reading this text, you\r\n";            $mime[] = "might want to consider changing to a mail reader that\r\n";            $mime[] = "understands how to properly display MIME multipart messages.\r\n\r\n";            $mime[] = sprintf("--Boundary-=%s\r\n", $this->boundary);            // Insert body. If multipart/alternative, insert both html and plain            $mime[] = sprintf("Content-Type: %s; charset = \"%s\";\r\n" .                              "\tboundary=\"Boundary-=%s\";\r\n\r\n",                               $this->ContentType, $this->CharSet, $this->subboundary);            $mime[] = sprintf("--Boundary-=%s\r\n", $this->subboundary);            $mime[] = sprintf("Content-Type: text/plain; charset = \"%s\";\r\n", $this->CharSet);            $mime[] = sprintf("Content-Transfer-Encoding: %s\r\n\r\n", $this->Encoding);            $mime[] = sprintf("%s\r\n\r\n", $this->AltBody);            $mime[] = sprintf("--Boundary-=%s\r\n", $this->subboundary);            $mime[] = sprintf("Content-Type: text/html; charset = \"%s\";\r\n", $this->CharSet);            $mime[] = sprintf("Content-Transfer-Encoding: %s\r\n\r\n", $this->Encoding);            $mime[] = sprintf("%s\r\n\r\n", $this->Body);            $mime[] = sprintf("\r\n--Boundary-=%s--\r\n\r\n", $this->subboundary);            $mime[] = sprintf("\r\n--Boundary-=%s--\r\n", $this->boundary);            $this->Body = $this->encode_string(join("", $mime), $this->Encoding);        }        else        {            $this->Body = $this->encode_string($this->Body, $this->Encoding);        }        if(count($this->attachment) > 0)        {            if(!$body = $this->attach_all())                return false;        }        else            $body = $this->Body;        return($body);    }    /////////////////////////////////////////////////    // ATTACHMENT METHODS    /////////////////////////////////////////////////    /**     * Adds an attachment from a path on the filesystem.     * Checks if attachment is valid and then adds     * the attachment to the list.     * Returns false if the file could not be found     * or accessed.     * @public     * @returns bool     */    function AddAttachment($path, $name = "", $encoding = "base64", $type = "application/octet-stream") {        if(!@is_file($path))        {            $this->error_handler(sprintf("Could not access [%s] file", $path));            return false;        }        $filename = basename($path);		//echo "filename : $filename <br>";		//echo "path : $path <br>";        if($name == "")            $name = $filename;        // Append to $attachment array        $cur = count($this->attachment);        $this->attachment[$cur][0] = $path;        $this->attachment[$cur][1] = $filename;        $this->attachment[$cur][2] = $name;        $this->attachment[$cur][3] = $encoding;        $this->attachment[$cur][4] = $type;        $this->attachment[$cur][5] = false; // isStringAttachment        return true;    }    /**     * Attaches all fs, string, and binary attachments to the message.     * Returns a string if successful or false if unsuccessful.     * @private     * @returns string     */    function attach_all() {        // Return text of body        $mime = array();        $mime[] = "This is a MIME message. If you are reading this text, you\r\n";        $mime[] = "might want to consider changing to a mail reader that\r\n";        $mime[] = "understands how to properly display MIME multipart messages.\r\n\r\n";        $mime[] = sprintf("--Boundary-=%s\r\n", $this->boundary);        // Insert body. If multipart/alternative, insert both html and plain.        if (!empty($this->AltBody))        {            $mime[] = sprintf("Content-Type: %s; charset = \"%s\";\r\n" .                              "\tboundary=\"Boundary-=%s\";\r\n\r\n",                               $this->ContentType, $this->CharSet, $this->subboundary);            $mime[] = sprintf("--Boundary-=%s\r\n", $this->subboundary);            $mime[] = sprintf("Content-Type: text/plain; charset = \"%s\";\r\n", $this->CharSet);            $mime[] = sprintf("Content-Transfer-Encoding: %s\r\n\r\n", $this->Encoding);            $mime[] = sprintf("%s\r\n\r\n", $this->AltBody);            $mime[] = sprintf("--Boundary-=%s\r\n", $this->subboundary);            $mime[] = sprintf("Content-Type: text/html; charset = \"%s\";\r\n", $this->CharSet);            $mime[] = sprintf("Content-Transfer-Encoding: %s\r\n\r\n", $this->Encoding);            $mime[] = sprintf("%s\r\n\r\n", $this->Body);            $mime[] = sprintf("\r\n--Boundary-=%s--\r\n\r\n", $this->subboundary);        }        else        {           $mime[] = sprintf("Content-Type: %s; charset = \"%s\";\r\n", $this->ContentType, $this->CharSet);           $mime[] = sprintf("Content-Transfer-Encoding: %s\r\n\r\n", $this->Encoding);           $mime[] = sprintf("%s\r\n", $this->Body);        }        // Add all attachments        for($i = 0; $i < count($this->attachment); $i++)        {          // Check for string attachment          $isString = $this->attachment[$i][5];          if ($isString)          {              $string = $this->attachment[$i][0];          }          else          {              $path = $this->attachment[$i][0];          }          $filename = $this->attachment[$i][1];          $name = $this->attachment[$i][2];          $encoding = $this->attachment[$i][3];		  //echo "encoding : $encoding <br>";          $type = $this->attachment[$i][4];          $mime[] = sprintf("--Boundary-=%s\r\n", $this->boundary);          $mime[] = sprintf("Content-Type: %s; ", $type);          $mime[] = sprintf("name=\"%s\"\r\n", $name);          $mime[] = sprintf("Content-Transfer-Encoding: %s\r\n", $encoding);          $mime[] = sprintf("Content-Disposition: attachment; filename=\"%s\"\r\n\r\n", $name);          // Encode as string attachment          if($isString)          {              if(!$mime[] = sprintf("%s\r\n\r\n", $this->encode_string($string, $encoding)))                return false;          }          else          {              if(!$mime[] = sprintf("%s\r\n\r\n", $this->encode_file($path, $encoding)))                return false;          }        }        $mime[] = sprintf("\r\n--Boundary-=%s--\r\n", $this->boundary);        return(join("", $mime));    }    /**     * Encodes attachment in requested format.  Returns a     * string if successful or false if unsuccessful.     * @private     * @returns string     */    function encode_file ($path, $encoding = "base64") {        if(!@$fd = fopen($path, "rb"))        {            $this->error_handler(sprintf("File Error: Could not open file %s", $path));            return false;        }        $file = fread($fd, filesize($path));        $encoded = $this->encode_string($file, $encoding);        fclose($fd);        return($encoded);    }    /**     * Encodes string to requested format. Returns a     * string if successful or false if unsuccessful.     * @private     * @returns string     */    function encode_string ($str, $encoding = "base64") {        switch(strtolower($encoding)) {          case "base64":              // chunk_split is found in PHP >= 3.0.6              $encoded = chunk_split(base64_encode($str));              break;          case "7bit":          case "8bit":              $encoded = $this->fix_eol($str);              if (substr($encoded, -2) != "\r\n")                $encoded .= "\r\n";              break;          case "binary":              $encoded = $str;              break;          case "quoted-printable":              $encoded = $this->encode_qp($str);              break;          default:              $this->error_handler(sprintf("Unknown encoding: %s", $encoding));              return false;        }        return($encoded);    }    /**     * Encode string to quoted-printable.  Returns a string.     * @private     * @returns string     */    function encode_qp ($str) {        $encoded = $this->fix_eol($str);        if (substr($encoded, -2) != "\r\n")            $encoded .= "\r\n";        // Replace every high ascii, control and = characters        $encoded = preg_replace("/([\001-\010\013\014\016-\037\075\177-\377])/e",                  "'='.sprintf('%02X', ord('\\1'))", $encoded);        // Replace every spaces and tabs when it's the last character on a line        $encoded = preg_replace("/([\011\040])\r\n/e",                  "'='.sprintf('%02X', ord('\\1')).'\r\n'", $encoded);        // Maximum line length of 76 characters before CRLF (74 + space + '=')        $encoded = $this->WordWrap($encoded, 74, true);        return $encoded;    }    /**    * Adds a string or binary attachment (non-filesystem) to the list.    * This method can be used to attach ascii or binary data,    * such as a BLOB record from a database.    * @public    * @returns void    */    function AddStringAttachment($string, $filename, $encoding = "base64", $type = "application/octet-stream") {        // Append to $attachment array        $cur = count($this->attachment);        $this->attachment[$cur][0] = $string;        $this->attachment[$cur][1] = $filename;        $this->attachment[$cur][2] = $filename;        $this->attachment[$cur][3] = $encoding;        $this->attachment[$cur][4] = $type;        $this->attachment[$cur][5] = true; // isString    }    /////////////////////////////////////////////////    // MESSAGE RESET METHODS    /////////////////////////////////////////////////    /**     * Clears all recipients assigned in the TO array.  Returns void.     * @public     * @returns void     */    function ClearAddresses() {        $this->to = array();    }    /**     * Clears all recipients assigned in the CC array.  Returns void.     * @public     * @returns void     */    function ClearCCs() {        $this->cc = array();    }    /**     * Clears all recipients assigned in the BCC array.  Returns void.     * @public     * @returns void     */    function ClearBCCs() {        $this->bcc = array();    }    /**     * Clears all recipients assigned in the ReplyTo array.  Returns void.     * @public     * @returns void     */    function ClearReplyTos() {        $this->ReplyTo = array();    }    /**     * Clears all recipients assigned in the TO, CC and BCC     * array.  Returns void.     * @public     * @returns void     */    function ClearAllRecipients() {        $this->to = array();        $this->cc = array();        $this->bcc = array();    }    /**     * Clears all previously set filesystem, string, and binary     * attachments.  Returns void.     * @public     * @returns void     */    function ClearAttachments() {        $this->attachment = array();    }    /**     * Clears all custom headers.  Returns void.     * @public     * @returns void     */    function ClearCustomHeaders() {        $this->CustomHeader = array();    }    /////////////////////////////////////////////////    // MISCELLANEOUS METHODS    /////////////////////////////////////////////////    /**     * Adds the error message to the error container.     * Returns void.     * @private     * @returns void     */    function error_handler($msg) {        $this->ErrorInfo = $msg;    }    /**     * Returns the proper RFC 822 formatted date. Returns string.     * @private     * @returns string     */    function rfc_date() {        $tz = date("Z");        $tzs = ($tz < 0) ? "-" : "+";        $tz = abs($tz);        $tz = ($tz/3600)*100 + ($tz%3600)/60;        $date = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);        return $date;    }    /**     * Returns received header for message tracing. Returns string.     * @private     * @returns string     */    function received() {        global $HTTP_SERVER_VARS;        global $HTTP_ENV_VARS;        // IIS & Apache use different global variables        if($HTTP_SERVER_VARS["REMOTE_ADDR"] == "")            $http_vars = $HTTP_ENV_VARS; // Apache found        else            $http_vars = $HTTP_SERVER_VARS; // IIS found		//$http_vars["REMOTE_ADDR"]        $str = sprintf("Received: from Mailing-List System [Version %s] " .               ";\r\n\t %s\r\n",               $this->Version,               $this->rfc_date());		/*$str = sprintf("Received: from Mailing-List System [Version %s] by %s " .               "with HTTP (%s);\r\n\t %s\r\n",               $this->Version,               $http_vars["SERVER_NAME"],               $http_vars["SERVER_SOFTWARE"],               $this->rfc_date());*/        return $str;    }    /**     * Changes every end of line from CR or LF to CRLF.  Returns string.     * @private     * @returns string     */    function fix_eol($str) {        $str = str_replace("\r\n", "\n", $str);        $str = str_replace("\r", "\n", $str);        $str = str_replace("\n", "\r\n", $str);        return $str;    }    /**     * Adds a custom header.  Returns void.     * @public     * @returns void     */    function AddCustomHeader($custom_header) {        $this->CustomHeader[] = $custom_header;    }    /**     * Adds all the Microsoft message headers.  Returns string.     * @private     * @returns string     */    function AddMSMailHeaders() {        $MSHeader = "";        if($this->Priority == 1)            $MSPriority = "High";        elseif($this->Priority == 5)            $MSPriority = "Low";        else            $MSPriority = "Medium";        $MSHeader .= sprintf("X-MSMail-Priority: %s\r\n", $MSPriority);        $MSHeader .= sprintf("Importance: %s\r\n", $MSPriority);        return($MSHeader);    }}// End of class?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?