Source for file mime.php
Documentation is available at mime.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +-----------------------------------------------------------------------+
// | Copyright (c) 2002-2003 Richard Heyes |
// | Copyright (c) 2003-2005 The PHP Group |
// | All rights reserved. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// +-----------------------------------------------------------------------+
// | Author: Richard Heyes <richard@phpguru.org> |
// | Tomas V.V.Cox <cox@idecnet.com> (port to PEAR) |
// +-----------------------------------------------------------------------+
// $Id: mime.php,v 1.39 2005/06/13 21:24:16 cipri Exp $
require_once('PEAR.php');
require_once('Mail/mimePart.php');
* Mime mail composer class. Can handle: text and html bodies, embedded html
* images and attachments.
* Documentation and examples of this class are avaible here:
* http://pear.php.net/manual/
* @notes This class is based on HTML Mime Mail class from
* Richard Heyes <richard@phpguru.org> which was based also
* in the mime_mail.class by Tobias Ratschiller <tobias@dnet.it> and
* Sascha Schumann <sascha@schumann.cx>
* @author Richard Heyes <richard.heyes@heyes-computing.net>
* @author Tomas V.V.Cox <cox@idecnet.com>
* Contains the plain text part of the email
* Contains the html part of the email
* contains the mime encoded text
* contains the multipart content
* list of the attached images
var $_html_images = array ();
* list of the attachements
var $_build_params = array ();
* End Of Line sequence (for serialize)
$this->_build_params = array (
'text_encoding' => '7bit',
'html_encoding' => 'quoted-printable',
'html_charset' => 'ISO-8859-1',
'text_charset' => 'ISO-8859-1',
'head_charset' => 'ISO-8859-1'
* Wakeup (unserialize) - re-sets EOL constant
$this->_setEOL ($this->_eol);
* Accessor function to set the body text. Body text is used if
* it's not an html mail being sent or else is used to fill the
* text/plain part that emails clients who don't support
* @param string $data Either a string or
* the file name with the contents
* @param bool $isfile If true the first param should be treated
* as a file name, else as a string (default)
* @param bool $append If true the text or file is appended to
* the existing body, else the old body is
* @return mixed true on success or PEAR_Error object
function setTXTBody($data, $isfile = false , $append = false )
$this->_txtbody .= $data;
$cont = $this->_file2str ($data);
if (PEAR ::isError ($cont)) {
$this->_txtbody .= $cont;
* Adds a html part to the mail
* @param string $data Either a string or the file name with the
* @param bool $isfile If true the first param should be treated
* as a file name, else as a string (default)
* @return mixed true on success or PEAR_Error object
$this->_htmlbody = $data;
$cont = $this->_file2str ($data);
if (PEAR ::isError ($cont)) {
$this->_htmlbody = $cont;
* Adds an image to the list of embedded images.
* @param string $file The image file name OR image data itself
* @param string $c_type The content type
* @param string $name The filename of the image.
* Only use if $file is the image data
* @param bool $isfilename Whether $file is a filename or not
* @return mixed true on success or PEAR_Error object
function addHTMLImage($file, $c_type= 'application/octet-stream',
$name = '', $isfilename = true )
$filedata = ($isfilename === true ) ? $this->_file2str ($file)
if ($isfilename === true ) {
if (PEAR ::isError ($filedata)) {
$this->_html_images[] = array (
* Adds a file to the list of attachments.
* @param string $file The file name of the file to attach
* OR the file data itself
* @param string $c_type The content type
* @param string $name The filename of the attachment
* Only use if $file is the file data
* @param bool $isFilename Whether $file is a filename or not
* @return mixed true on success or PEAR_Error object
function addAttachment($file, $c_type = 'application/octet-stream',
$name = '', $isfilename = true ,
$filedata = ($isfilename === true ) ? $this->_file2str ($file)
if ($isfilename === true ) {
// Force the name the user supplied, otherwise use $file
$filename = (!empty ($name)) ? $name : $file;
'The supplied filename for the attachment can\'t be empty'
if (PEAR ::isError ($filedata)) {
* Get the contents of the given file name as string
* @param string $file_name path of file to process
* @return string contents of $file_name
function &_file2str ($file_name)
return PEAR ::raiseError ('File is not readable ' . $file_name);
if (!$fd = fopen($file_name, 'rb')) {
return PEAR ::raiseError ('Could not open ' . $file_name);
$cont = fread($fd, $filesize);
* Adds a text subpart to the mimePart object 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.
* @param string The text to add.
* @return object The text mimePart object
function &_addTextPart (&$obj, $text)
$params['content_type'] = 'text/plain';
$params['encoding'] = $this->_build_params['text_encoding'];
$params['charset'] = $this->_build_params['text_charset'];
return $obj->addSubpart ($text, $params);
* Adds a html subpart to the mimePart object 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 html mimePart object
function &_addHtmlPart (&$obj)
$params['content_type'] = 'text/html';
$params['encoding'] = $this->_build_params['html_encoding'];
$params['charset'] = $this->_build_params['html_charset'];
return $obj->addSubpart ($this->_htmlbody, $params);
* Creates a new mimePart object, using multipart/mixed as
* the initial content-type and returns it during the
* @return object The multipart/mixed mimePart object
function &_addMixedPart ()
$params['content_type'] = 'multipart/mixed';
* Adds a multipart/alternative part to a mimePart
* object (or creates one), and returns it during
* @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
function &_addAlternativePart (&$obj)
$params['content_type'] = 'multipart/alternative';
return $obj->addSubpart ('', $params);
* Adds a multipart/related part to a mimePart
* object (or creates one), and returns it during
* @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
function &_addRelatedPart (&$obj)
$params['content_type'] = 'multipart/related';
return $obj->addSubpart ('', $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
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
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
* html_encoding - What encoding to use for html
* Default is quoted-printable
* 7bit_wrap - Number of characters before text is
* wrapped in 7bit encoding
* html_charset - The character set to use for html.
* text_charset - The character set to use for text.
* head_charset - The character set to use for headers.
* @return string The mime content
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'], '#') .
$rep = '\1\2=\3cid:' . $value['cid'] . '\3';
$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;
case $text AND !$attachments:
$message = & $this->_addTextPart ($null, $this->_txtbody);
case !$text AND !$html AND $attachments:
$message = & $this->_addMixedPart ();
for ($i = 0; $i < count($this->_parts); $i++ ) {
$this->_addAttachmentPart ($message, $this->_parts[$i]);
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]);
case $html AND !$attachments AND !$html_images:
if (isset ($this->_txtbody)) {
$message = & $this->_addAlternativePart ($null);
$this->_addTextPart ($message, $this->_txtbody);
$this->_addHtmlPart ($message);
$message = & $this->_addHtmlPart ($null);
case $html AND !$attachments AND $html_images:
if (isset ($this->_txtbody)) {
$message = & $this->_addAlternativePart ($null);
$this->_addTextPart ($message, $this->_txtbody);
$related = & $this->_addRelatedPart ($message);
$message = & $this->_addRelatedPart ($null);
$this->_addHtmlPart ($related);
for ($i = 0; $i < count($this->_html_images); $i++ ) {
$this->_addHtmlImagePart ($related, $this->_html_images[$i]);
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);
$this->_addHtmlPart ($message);
for ($i = 0; $i < count($this->_parts); $i++ ) {
$this->_addAttachmentPart ($message, $this->_parts[$i]);
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);
$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]);
$output = $message->encode ();
* 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.
* @return array Assoc array with the mime headers
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)) {
$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.
* @return string Plain text headers
$headers = $this->headers($xtra_headers);
foreach ($headers as $key => $val) {
$ret .= " $key: $val" . MAIL_MIME_CRLF;
* Sets the Subject header
* @param string $subject String to set the subject to
$this->_headers['Subject'] = $subject;
* Set an email to the From (the sender) header
* @param string $email The email direction to add
$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
if (isset ($this->_headers['Cc'])) {
$this->_headers['Cc'] .= " , $email";
$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
if (isset ($this->_headers['Bcc'])) {
$this->_headers['Bcc'] .= " , $email";
$this->_headers['Bcc'] = $email;
* Encodes a header as per RFC2047
* @param string $input The header data to encode
* @return string Encoded data
function _encodeHeaders ($input)
foreach ($input as $hdr_name => $hdr_value) {
foreach ($matches[1 ] as $value) {
strtoupper(dechex(ord("\1")))',
$this->_build_params['head_charset'] .
'?Q?' . $replacement . '?=',
$input[$hdr_name] = $hdr_value;
* Set the object's end-of-line and define the constant if applicable
* @param string $eol End Of Line sequence
define('MAIL_MIME_CRLF', $this->_eol, true );
Documentation generated on Mon, 11 Mar 2019 14:18:33 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|