📄 mime.php
字号:
/** * Adds a multipart/alternative part to a mimePart * object (or creates one), and returns it during * the build process. * * @param mixed The object to add the part to, or * null if a new object is to be created. * @return object The multipart/mixed mimePart object * @access private */ function &_addAlternativePart(&$obj) { $params['content_type'] = 'multipart/alternative'; if (is_object($obj)) { return $obj->addSubpart('', $params); } else { return new Mail_mimePart('', $params); } } /** * Adds a multipart/related part to a mimePart * object (or creates one), and returns it during * the build process. * * @param mixed The object to add the part to, or * null if a new object is to be created * @return object The multipart/mixed mimePart object * @access private */ function &_addRelatedPart(&$obj) { $params['content_type'] = 'multipart/related'; if (is_object($obj)) { return $obj->addSubpart('', $params); } else { return new Mail_mimePart('', $params); } } /** * Adds an html image subpart to a mimePart object * and returns it during the build process. * * @param object The mimePart to add the image to * @param array The image information * @return object The image mimePart object * @access private */ function &_addHtmlImagePart(&$obj, $value) { $params['content_type'] = $value['c_type']; $params['encoding'] = 'base64'; $params['disposition'] = 'inline'; $params['dfilename'] = $value['name']; $params['cid'] = $value['cid']; $obj->addSubpart($value['body'], $params); } /** * Adds an attachment subpart to a mimePart object * and returns it during the build process. * * @param object The mimePart to add the image to * @param array The attachment information * @return object The image mimePart object * @access private */ function &_addAttachmentPart(&$obj, $value) { $params['content_type'] = $value['c_type']; $params['encoding'] = $value['encoding']; $params['disposition'] = 'attachment'; $params['dfilename'] = $value['name']; $obj->addSubpart($value['body'], $params); } /** * Builds the multipart message from the list ($this->_parts) and * returns the mime content. * * @param array Build parameters that change the way the email * is built. Should be associative. Can contain: * text_encoding - What encoding to use for plain text * Default is 7bit * html_encoding - What encoding to use for html * Default is quoted-printable * 7bit_wrap - Number of characters before text is * wrapped in 7bit encoding * Default is 998 * html_charset - The character set to use for html. * Default is iso-8859-1 * text_charset - The character set to use for text. * Default is iso-8859-1 * head_charset - The character set to use for headers. * Default is iso-8859-1 * @return string The mime content * @access public */ function &get($build_params = null) { if (isset($build_params)) { while (list($key, $value) = each($build_params)) { $this->_build_params[$key] = $value; } } if (!empty($this->_html_images) AND isset($this->_htmlbody)) { foreach ($this->_html_images as $value) { $regex = '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' . preg_quote($value['name'], '#') . '\3#'; $rep = '\1\2=\3cid:' . $value['cid'] .'\3'; $this->_htmlbody = preg_replace($regex, $rep, $this->_htmlbody ); } } $null = null; $attachments = !empty($this->_parts) ? true : false; $html_images = !empty($this->_html_images) ? true : false; $html = !empty($this->_htmlbody) ? true : false; $text = (!$html AND !empty($this->_txtbody)) ? true : false; switch (true) { case $text AND !$attachments: $message =& $this->_addTextPart($null, $this->_txtbody); break; case !$text AND !$html AND $attachments: $message =& $this->_addMixedPart(); for ($i = 0; $i < count($this->_parts); $i++) { $this->_addAttachmentPart($message, $this->_parts[$i]); } break; case $text AND $attachments: $message =& $this->_addMixedPart(); $this->_addTextPart($message, $this->_txtbody); for ($i = 0; $i < count($this->_parts); $i++) { $this->_addAttachmentPart($message, $this->_parts[$i]); } break; case $html AND !$attachments AND !$html_images: if (isset($this->_txtbody)) { $message =& $this->_addAlternativePart($null); $this->_addTextPart($message, $this->_txtbody); $this->_addHtmlPart($message); } else { $message =& $this->_addHtmlPart($null); } break; case $html AND !$attachments AND $html_images: if (isset($this->_txtbody)) { $message =& $this->_addAlternativePart($null); $this->_addTextPart($message, $this->_txtbody); $related =& $this->_addRelatedPart($message); } else { $message =& $this->_addRelatedPart($null); $related =& $message; } $this->_addHtmlPart($related); for ($i = 0; $i < count($this->_html_images); $i++) { $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(); $output['headers'][] = 'Mime-Version: 1.0'; $this->_headers = array_merge($this->_headers, $output['headers']); return $output['body']; } else { return false; } } /** * 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. * @return array Assoc array with the mime headers * @access public */ function &headers($xtra_headers = null) { // 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); } $this->_headers = array_merge($headers, $this->_headers); return $this->_encodeHeaders($this->_headers); } /** * 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. * @return string Plain text headers * @access public */ function txtHeaders($xtra_headers = null) { $headers = $this->headers($xtra_headers); $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; } } /** * Encodes a header as per RFC2047 * * @param string $input The header data to encode * @return string Encoded data * @access private */ function _encodeHeaders($input) { foreach ($input as $hdr_name => $hdr_value) { preg_match_all('/(\w*[\x80-\xFF]+\w*)/', $hdr_value, $matches); foreach ($matches[1] as $value) { $replacement = preg_replace('/([\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value); $hdr_value = str_replace($value, '=?' . $this->_build_params['head_charset'] . '?Q?' . $replacement . '?=', $hdr_value); } $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 * @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 + -