Source for file mime.php
Documentation is available at mime.php
* The Mail_Mime class is used to create MIME E-mail messages
* The Mail_Mime class provides an OO interface to create MIME
* enabled email messages. This way you can create emails that
* contain plain-text bodies, HTML bodies, attachments, inline
* images and specific headers.
* Compatible with PHP versions 4 and 5
* LICENSE: This LICENSE is in the BSD license style.
* Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org>
* Copyright (c) 2003-2006, PEAR <pear-group@php.net>
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - 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.
* - Neither the name of the authors, nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
* 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>
* @author Tomas V.V. Cox <cox@idecnet.com>
* @author Cipriano Groenendal <cipri@php.net>
* @author Sean Coates <sean@php.net>
* @copyright 2003-2006 PEAR <pear-group@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version CVS: $Id: mime.php,v 1.81 2007/06/21 19:08:28 cipri Exp $
* @link http://pear.php.net/package/Mail_mime
* 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>
* This package depends on PEAR to raise errors.
* Mail_mimePart contains the code required to
* create all the different parts a mail can
require_once 'Mail/mimePart.php';
* The Mail_Mime class provides an OO interface to create MIME
* enabled email messages. This way you can create emails that
* contain plain-text bodies, HTML bodies, attachments, inline
* images and specific headers.
* @author Richard Heyes <richard@phpguru.org>
* @author Tomas V.V. Cox <cox@idecnet.com>
* @author Cipriano Groenendal <cipri@php.net>
* @author Sean Coates <sean@php.net>
* @copyright 2003-2006 PEAR <pear-group@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version Release: @package_version@
* @link http://pear.php.net/package/Mail_mime
* 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)
* @param string $crlf what type of linebreak to use.
$this->_build_params = array (
'head_encoding' => 'quoted-printable',
'text_encoding' => '7bit',
'html_encoding' => 'quoted-printable',
'html_charset' => 'ISO-8859-1',
'text_charset' => 'ISO-8859-1',
'head_charset' => 'ISO-8859-1'
* wakeup function called by unserialize. It re-sets the 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 a flag that determines whether $data is a
* filename, or a string(false, default)
* @return bool true on success
$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 used if $file is the image data.
* @param bool $isfile whether $file is a filename or not.
* @return bool true on success
function addHTMLImage($file, $c_type= 'application/octet-stream',
$name = '', $isfile = true )
$filedata = ($isfile === true ) ? $this->_file2str ($file)
$filename = ($name == '' ? $file : $name);
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 contents itself
* @param string $c_type The content type
* @param string $name The filename of the attachment
* Only use if $file is the contents
* @param bool $isfile Whether $file is a filename or not
* @param string $encoding The type of encoding to use.
* Possible values: 7bit, 8bit, base64,
* @param string $disposition The content-disposition of this file
* Defaults to attachment.
* Possible values: attachment, inline.
* @param string $charset The character set used in the filename
* @param string $language The language of the attachment
* @param string $location The RFC 2557.4 location of the attachment
* @return mixed true on success or PEAR_Error object
$c_type = 'application/octet-stream',
$disposition = 'attachment',
$filedata = ($isfile === true ) ? $this->_file2str ($file)
// Force the name the user supplied, otherwise use $file
$filename = (strlen($name)) ? $name : $file;
$msg = "The supplied filename for the attachment can't be empty";
$err = PEAR ::raiseError ($msg);
if (PEAR ::isError ($filedata)) {
'disposition' => $disposition
* 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)
$err = PEAR ::raiseError ('File is not readable ' . $file_name);
if (!$fd = fopen($file_name, 'rb')) {
$err = PEAR ::raiseError ('Could not open ' . $file_name);
$cont = fread($fd, $filesize);
if ($magic_quote_setting) {
* Adds a text subpart to the mimePart object and
* returns it during the build process.
* @param mixed &$obj The object to add the part to, or
* null if a new object is to be created.
* @param string $text 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'];
$ret = $obj->addSubpart ($text, $params);
* Adds a html subpart to the mimePart object and
* returns it during the build process.
* @param mixed &$obj 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'];
$ret = $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';
//Create empty multipart/mixed Mail_mimePart object to return
* Adds a multipart/alternative part to a mimePart
* object (or creates one), and returns it during
* @param mixed &$obj 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 &$obj 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 &$obj The mimePart to add the image to
* @param array $value 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'];
$ret = $obj->addSubpart ($value['body'], $params);
* Adds an attachment subpart to a mimePart object
* and returns it during the build process.
* @param object &$obj The mimePart to add the image to
* @param array $value The attachment information
* @return object The image mimePart object
function &_addAttachmentPart (&$obj, $value)
$params['dfilename'] = $value['name'];
$params['encoding'] = $value['encoding'];
$params['charset'] = $value['charset'];
if ($value['language']) {
$params['language'] = $value['language'];
if ($value['location']) {
$params['location'] = $value['location'];
$params['content_type'] = $value['c_type'];
$params['disposition'] = isset ($value['disposition']) ?
$value['disposition'] : 'attachment';
$ret = $obj->addSubpart ($value['body'], $params);
* Returns the complete e-mail, ready to send using an alternative
* mail delivery method. Note that only the mailpart that is made
* with Mail_Mime is created. This means that,
* YOU WILL HAVE NO TO: HEADERS UNLESS YOU SET IT YOURSELF
* using the $xtra_headers parameter!
* @param string $separation The separation etween these two parts.
* @param array $build_params The Build parameters passed to the
* &get() function. See &get for more info.
* @param array $xtra_headers The extra headers that should be passed
* to the &headers() function.
* See that function for more info.
* @param bool $overwrite Overwrite the existing headers with new.
* @return string The complete e-mail.
if ($separation === null ) {
$separation = MAIL_MIME_CRLF;
$body = $this->get($build_params);
$head = $this->txtHeaders($xtra_headers, $overwrite);
$mail = $head . $separation . $body;
* Builds the multipart message from the list ($this->_parts) and
* returns the mime content.
* @param array $build_params Build parameters that change the way the email
* is built. Should be associative. Can contain:
* head_encoding - What encoding to use for the headers.
* Options: quoted-printable or base64
* Default is quoted-printable
* text_encoding - What encoding to use for plain text
|