📄 mime.php
字号:
$this->_addHtmlImagePart($related, $this->_html_images[$i]); } break; case $html AND $attachments AND !$html_images: $message =& $this->_addMixedPart(); if (isset($this->_txtbody)) { $alt =& $this->_addAlternativePart($message); $this->_addTextPart($alt, $this->_txtbody); $this->_addHtmlPart($alt); } else { $this->_addHtmlPart($message); } for ($i = 0; $i < count($this->_parts); $i++) { $this->_addAttachmentPart($message, $this->_parts[$i]); } break; 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 * access public */ function setSubject($subject) { $this->_headers['Subject'] = $subject; } /** * Set an email to the From (the sender) header * * @param string $email The email direction to add * @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 * @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 * @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; } foreach ($input as $hdr_name => $hdr_value) { $hdr_vals = preg_split("|(\s)|", $hdr_value, -1, PREG_SPLIT_DELIM_CAPTURE); $hdr_value_out=""; $previous = ""; foreach ($hdr_vals as $hdr_val){ if (!trim($hdr_val)){ //whitespace needs to be handled with another string, or it //won't show between encoded strings. Prepend this to the next item. $previous .= $hdr_val; continue; }else{ $hdr_val = $previous . $hdr_val; $previous = ""; } if (function_exists('iconv_mime_encode') && preg_match('#[\x80-\xFF]{1}#', $hdr_val)){ $imePref = 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']; $hdr_val = iconv_mime_encode($hdr_name, $hdr_val, $imePrefs); $hdr_val = preg_replace("#^{$hdr_name}\:\ #", "", $hdr_val); }elseif (preg_match('#[\x80-\xFF]{1}#', $hdr_val)){ //This header contains non ASCII chars and should be encoded. switch ($build_params['head_encoding']) { case 'base64': //Base64 encoding has been selected. //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; //Base64 encode the entire string $hdr_val = base64_encode($hdr_val); //This regexp will break base64-encoded text at every //$maxLength but will not break any encoded letters. $reg1st = "|.{0,$maxLength1stLine}[^\=][^\=]|"; $reg2nd = "|.{0,$maxLength}[^\=][^\=]|"; break; case 'quoted-printable': default: //quoted-printable encoding has been selected //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; $maxLength1stLine = $maxLength - strlen($hdr_name) - 2; //Replace all special characters used by the encoder. $search = array("=", "_", "?", " "); $replace = array("=3D", "=5F", "=3F", "_"); $hdr_val = str_replace($search, $replace, $hdr_val); //Replace all extended characters (\x80-xFF) with their //ASCII values. $hdr_val = preg_replace( '#([\x80-\xFF])#e', '"=" . strtoupper(dechex(ord("\1")))', $hdr_val ); //This regexp will break QP-encoded text at every $maxLength //but will not break any encoded letters. $reg1st = "|(.{0,$maxLength1stLine})[^\=]|"; $reg2nd = "|(.{0,$maxLength})[^\=]|"; break; } //Begin with the regexp for the first line. $reg = $reg1st; //Prevent lins that are just way to short; if ($maxLength1stLine >1){ $reg = $reg2nd; } $output = ""; while ($hdr_val) { //Split translated string at every $maxLength //But make sure not to break any translated chars. $found = preg_match($reg, $hdr_val, $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_val variable. if ($found){ $part = $matches[0]; $hdr_val = substr($hdr_val, strlen($matches[0])); }else{ $part = $hdr_val; $hdr_val = ""; } //RFC 2047 specifies that any split header should be seperated //by a CRLF SPACE. if ($output){ $output .= "\r\n "; } $output .= $prefix . $part . $suffix; } $hdr_val = $output; } $hdr_value_out .= $hdr_val; } $input[$hdr_name] = $hdr_value_out; } return $input; } /** * Set the object's end-of-line and define the constant if applicable * * @param string $eol End Of Line sequence * @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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -