mime.php

来自「This is the script which used on 10minut」· PHP 代码 · 共 1,096 行 · 第 1/3 页

PHP
1,096
字号
        case $html AND $attachments AND $html_images:            $message =& $this->_addMixedPart();            if (isset($this->_txtbody)) {                $alt =& $this->_addAlternativePart($message);                $this->_addTextPart($alt, $this->_txtbody);                $rel =& $this->_addRelatedPart($alt);            } else {                $rel =& $this->_addRelatedPart($message);            }            $this->_addHtmlPart($rel);            for ($i = 0; $i < count($this->_html_images); $i++) {                $this->_addHtmlImagePart($rel, $this->_html_images[$i]);            }            for ($i = 0; $i < count($this->_parts); $i++) {                $this->_addAttachmentPart($message, $this->_parts[$i]);            }            break;        }        if (isset($message)) {            $output = $message->encode();                        $this->_headers = array_merge($this->_headers,                                          $output['headers']);            $body = $output['body'];            return $body;        } else {            $ret = false;            return $ret;        }    }    /**     * Returns an array with the headers needed to prepend to the email     * (MIME-Version and Content-Type). Format of argument is:     * $array['header-name'] = 'header-value';     *     * @param array $xtra_headers Assoc array with any extra headers.     *                             Optional.     * @param bool  $overwrite    Overwrite already existing headers.     *      * @return array Assoc array with the mime headers     * @access public     */    function &headers($xtra_headers = null, $overwrite = false)    {        // Content-Type header should already be present,        // So just add mime version header        $headers['MIME-Version'] = '1.0';        if (isset($xtra_headers)) {            $headers = array_merge($headers, $xtra_headers);        }        if ($overwrite) {            $this->_headers = array_merge($this->_headers, $headers);        } else {            $this->_headers = array_merge($headers, $this->_headers);        }        $encodedHeaders = $this->_encodeHeaders($this->_headers);        return $encodedHeaders;    }    /**     * Get the text version of the headers     * (usefull if you want to use the PHP mail() function)     *     * @param array $xtra_headers Assoc array with any extra headers.     *                             Optional.     * @param bool  $overwrite    Overwrite the existing heaers with new.     *     * @return string  Plain text headers     * @access public     */    function txtHeaders($xtra_headers = null, $overwrite = false)    {        $headers = $this->headers($xtra_headers, $overwrite);                $ret = '';        foreach ($headers as $key => $val) {            $ret .= "$key: $val" . MAIL_MIME_CRLF;        }        return $ret;    }    /**     * Sets the Subject header     *     * @param string $subject String to set the subject to.     *     * @return void     * @access public     */    function setSubject($subject)    {        $this->_headers['Subject'] = $subject;    }    /**     * Set an email to the From (the sender) header     *     * @param string $email The email address to use     *     * @return void     * @access public     */    function setFrom($email)    {        $this->_headers['From'] = $email;    }    /**     * Add an email to the Cc (carbon copy) header     * (multiple calls to this method are allowed)     *     * @param string $email The email direction to add     *     * @return void     * @access public     */    function addCc($email)    {        if (isset($this->_headers['Cc'])) {            $this->_headers['Cc'] .= ", $email";        } else {            $this->_headers['Cc'] = $email;        }    }    /**     * Add an email to the Bcc (blank carbon copy) header     * (multiple calls to this method are allowed)     *     * @param string $email The email direction to add     *     * @return void     * @access public     */    function addBcc($email)    {        if (isset($this->_headers['Bcc'])) {            $this->_headers['Bcc'] .= ", $email";        } else {            $this->_headers['Bcc'] = $email;        }    }    /**     * Since the PHP send function requires you to specifiy      * recipients (To: header) separately from the other     * headers, the To: header is not properly encoded.     * To fix this, you can use this public method to      * encode your recipients before sending to the send     * function     *     * @param string $recipients A comma-delimited list of recipients     *     * @return string Encoded data     * @access public     */    function encodeRecipients($recipients)    {        $input = array("To" => $recipients);        $retval = $this->_encodeHeaders($input);        return $retval["To"] ;    }    /**     * Encodes a header as per RFC2047     *     * @param array $input  The header data to encode     * @param array $params Extra build parameters     *     * @return array Encoded data     * @access private     */    function _encodeHeaders($input, $params = array())    {                $build_params = $this->_build_params;        while (list($key, $value) = each($params)) {            $build_params[$key] = $value;        }        //$hdr_name: Name of the heaer        //$hdr_value: Full line of header value.        //$hdr_value_out: The recombined $hdr_val-atoms, or the encoded string.                        $useIconv = true;                if (isset($build_params['ignore-iconv'])) {            $useIconv = !$build_params['ignore-iconv'];        }                    foreach ($input as $hdr_name => $hdr_value) {            if (preg_match('#([\x80-\xFF]){1}#', $hdr_value)) {                if (function_exists('iconv_mime_encode') && $useIconv) {                    $imePrefs = array();                    if ($build_params['head_encoding'] == 'base64') {                        $imePrefs['scheme'] = 'B';                    } else {                        $imePrefs['scheme'] = 'Q';                    }                    $imePrefs['input-charset']  = $build_params['head_charset'];                    $imePrefs['output-charset'] = $build_params['head_charset'];                    $imePrefs['line-length'] = 74;                    $imePrefs['line-break-chars'] = "\r\n"; //Specified in RFC2047                                        $hdr_value = iconv_mime_encode($hdr_name, $hdr_value, $imePrefs);                    $hdr_value = preg_replace("#^{$hdr_name}\:\ #", "", $hdr_value);                } elseif ($build_params['head_encoding'] == 'base64') {                    //Base64 encoding has been selected.                    //Base64 encode the entire string                    $hdr_value = base64_encode($hdr_value);                                        //Generate the header using the specified params and dynamicly                     //determine the maximum length of such strings.                    //75 is the value specified in the RFC. The first -2 is there so                     //the later regexp doesn't break any of the translated chars.                    //The -2 on the first line-regexp is to compensate for the ": "                    //between the header-name and the header value                    $prefix = '=?' . $build_params['head_charset'] . '?B?';                    $suffix = '?=';                    $maxLength = 75 - strlen($prefix . $suffix) - 2;                    $maxLength1stLine = $maxLength - strlen($hdr_name) - 2;                    //We can cut base4 every 4 characters, so the real max                    //we can get must be rounded down.                    $maxLength = $maxLength - ($maxLength % 4);                    $maxLength1stLine = $maxLength1stLine - ($maxLength1stLine % 4);                                        $cutpoint = $maxLength1stLine;                    $hdr_value_out = $hdr_value;                    $output = "";                    while ($hdr_value_out) {                        //Split translated string at every $maxLength                        $part = substr($hdr_value_out, 0, $cutpoint);                        $hdr_value_out = substr($hdr_value_out, $cutpoint);                        $cutpoint = $maxLength;                        //RFC 2047 specifies that any split header should                         //be seperated by a CRLF SPACE.                         if ($output) {                            $output .=  "\r\n ";                        }                        $output .= $prefix . $part . $suffix;                    }                    $hdr_value = $output;                } else {                    //quoted-printable encoding has been selected                    //Fix for Bug #10298, Ota Mares <om@viazenetti.de>                    //Check if there is a double quote at beginning or end of                    //the string to prevent that an open or closing quote gets                     //ignored because it is encapsuled by an encoding pre/suffix.                    //Remove the double quote and set the specific prefix or                     //suffix variable so that we can concat the encoded string and                    //the double quotes back together to get the intended string.                    $quotePrefix = $quoteSuffix = '';                    if ($hdr_value{0} == '"') {                        $hdr_value = substr($hdr_value, 1);                        $quotePrefix = '"';                    }                    if ($hdr_value{strlen($hdr_value)-1} == '"') {                        $hdr_value = substr($hdr_value, 0, -1);                        $quoteSuffix = '"';                    }                                        //Generate the header using the specified params and dynamicly                     //determine the maximum length of such strings.                    //75 is the value specified in the RFC. The -2 is there so                     //the later regexp doesn't break any of the translated chars.                    //The -2 on the first line-regexp is to compensate for the ": "                    //between the header-name and the header value                    $prefix = '=?' . $build_params['head_charset'] . '?Q?';                    $suffix = '?=';                    $maxLength = 75 - strlen($prefix . $suffix) - 2 - 1;                    $maxLength1stLine = $maxLength - strlen($hdr_name) - 2;                    $maxLength = $maxLength - 1;                                        //Replace all special characters used by the encoder.                    $search  = array('=',   '_',   '?',   ' ');                    $replace = array('=3D', '=5F', '=3F', '_');                    $hdr_value = str_replace($search, $replace, $hdr_value);                                        //Replace all extended characters (\x80-xFF) with their                    //ASCII values.                    $hdr_value = preg_replace('#([\x80-\xFF])#e',                        '"=" . strtoupper(dechex(ord("\1")))',                        $hdr_value);                    //This regexp will break QP-encoded text at every $maxLength                    //but will not break any encoded letters.                    $reg1st = "|(.{0,$maxLength1stLine}[^\=][^\=])|";                    $reg2nd = "|(.{0,$maxLength}[^\=][^\=])|";                    //Fix for Bug #10298, Ota Mares <om@viazenetti.de>                    //Concat the double quotes and encoded string together                    $hdr_value = $quotePrefix . $hdr_value . $quoteSuffix;                                        $hdr_value_out = $hdr_value;                    $realMax = $maxLength1stLine + strlen($prefix . $suffix);                    if (strlen($hdr_value_out) >= $realMax) {                        //Begin with the regexp for the first line.                        $reg = $reg1st;                        $output = "";                        while ($hdr_value_out) {                            //Split translated string at every $maxLength                            //But make sure not to break any translated chars.                            $found = preg_match($reg, $hdr_value_out, $matches);                                                        //After this first line, we need to use a different                            //regexp for the first line.                            $reg = $reg2nd;                                                        //Save the found part and encapsulate it in the                            //prefix & suffix. Then remove the part from the                            //$hdr_value_out variable.                            if ($found) {                                $part = $matches[0];                                $len = strlen($matches[0]);                                $hdr_value_out = substr($hdr_value_out, $len);                            } else {                                $part = $hdr_value_out;                                $hdr_value_out = "";                            }                                                        //RFC 2047 specifies that any split header should                             //be seperated by a CRLF SPACE                            if ($output) {                                $output .=  "\r\n ";                            }                            $output .= $prefix . $part . $suffix;                        }                        $hdr_value_out = $output;                    } else {                        $hdr_value_out = $prefix . $hdr_value_out . $suffix;                    }                    $hdr_value = $hdr_value_out;                }            }            $input[$hdr_name] = $hdr_value;        }        return $input;    }    /**     * Set the object's end-of-line and define the constant if applicable.     *     * @param string $eol End Of Line sequence     *     * @return void     * @access private     */    function _setEOL($eol)    {        $this->_eol = $eol;        if (!defined('MAIL_MIME_CRLF')) {            define('MAIL_MIME_CRLF', $this->_eol, true);        }    }    } // End of class

⌨️ 快捷键说明

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