Mail_Mime
[ class tree: Mail_Mime ] [ index: Mail_Mime ] [ all elements ]

Source for file mime.php

Documentation is available at mime.php

  1. <?php
  2. /**
  3.  * The Mail_Mime class is used to create MIME E-mail messages
  4.  *
  5.  * The Mail_Mime class provides an OO interface to create MIME
  6.  * enabled email messages. This way you can create emails that
  7.  * contain plain-text bodies, HTML bodies, attachments, inline
  8.  * images and specific headers.
  9.  *
  10.  * Compatible with PHP versions 4 and 5
  11.  *
  12.  * LICENSE: This LICENSE is in the BSD license style.
  13.  * Copyright (c) 2002-2003, Richard Heyes <richard@phpguru.org>
  14.  * Copyright (c) 2003-2006, PEAR <pear-group@php.net>
  15.  * All rights reserved.
  16.  *
  17.  * Redistribution and use in source and binary forms, with or
  18.  * without modification, are permitted provided that the following
  19.  * conditions are met:
  20.  *
  21.  * - Redistributions of source code must retain the above copyright
  22.  *   notice, this list of conditions and the following disclaimer.
  23.  * - Redistributions in binary form must reproduce the above copyright
  24.  *   notice, this list of conditions and the following disclaimer in the
  25.  *   documentation and/or other materials provided with the distribution.
  26.  * - Neither the name of the authors, nor the names of its contributors
  27.  *   may be used to endorse or promote products derived from this
  28.  *   software without specific prior written permission.
  29.  *
  30.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  34.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  37.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  38.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  40.  * THE POSSIBILITY OF SUCH DAMAGE.
  41.  *
  42.  * @category   Mail
  43.  * @package    Mail_Mime
  44.  * @author     Richard Heyes  <richard@phpguru.org>
  45.  * @author     Tomas V.V. Cox <cox@idecnet.com>
  46.  * @author     Cipriano Groenendal <cipri@php.net>
  47.  * @author     Sean Coates <sean@php.net>
  48.  * @copyright  2003-2006 PEAR <pear-group@php.net>
  49.  * @license    http://www.opensource.org/licenses/bsd-license.php BSD License
  50.  * @version    CVS: $Id: mime.php,v 1.72 2007/04/28 12:31:53 cipri Exp $
  51.  * @link       http://pear.php.net/package/Mail_mime
  52.  * @notes      This class is based on HTML Mime Mail class from
  53.  *              Richard Heyes <richard@phpguru.org> which was based also
  54.  *              in the mime_mail.class by Tobias Ratschiller <tobias@dnet.it>
  55.  *              and Sascha Schumann <sascha@schumann.cx>
  56.  */
  57.  
  58.  
  59. /**
  60.  * require PEAR
  61.  *
  62.  * This package depends on PEAR to raise errors.
  63.  */
  64. require_once('PEAR.php');
  65.  
  66. /**
  67.  * require Mail_mimePart
  68.  *
  69.  * Mail_mimePart contains the code required to
  70.  * create all the different parts a mail can
  71.  * consist of.
  72.  */
  73. require_once('Mail/mimePart.php');
  74.  
  75.  
  76. /**
  77.  * The Mail_Mime class provides an OO interface to create MIME
  78.  * enabled email messages. This way you can create emails that
  79.  * contain plain-text bodies, HTML bodies, attachments, inline
  80.  * images and specific headers.
  81.  *
  82.  * @category   Mail
  83.  * @package    Mail_Mime
  84.  * @author     Richard Heyes  <richard@phpguru.org>
  85.  * @author     Tomas V.V. Cox <cox@idecnet.com>
  86.  * @author     Cipriano Groenendal <cipri@php.net>
  87.  * @author     Sean Coates <sean@php.net>
  88.  * @copyright  2003-2006 PEAR <pear-group@php.net>
  89.  * @license    http://www.opensource.org/licenses/bsd-license.php BSD License
  90.  * @version    Release: @package_version@
  91.  * @link       http://pear.php.net/package/Mail_mime
  92.  */
  93. class Mail_mime
  94. {
  95.     /**
  96.      * Contains the plain text part of the email
  97.      *
  98.      * @var string 
  99.      * @access private
  100.      */
  101.     var $_txtbody;
  102.  
  103.     /**
  104.      * Contains the html part of the email
  105.      *
  106.      * @var string 
  107.      * @access private
  108.      */
  109.     var $_htmlbody;
  110.  
  111.     /**
  112.      * contains the mime encoded text
  113.      *
  114.      * @var string 
  115.      * @access private
  116.      */
  117.     var $_mime;
  118.  
  119.     /**
  120.      * contains the multipart content
  121.      *
  122.      * @var string 
  123.      * @access private
  124.      */
  125.     var $_multipart;
  126.  
  127.     /**
  128.      * list of the attached images
  129.      *
  130.      * @var array 
  131.      * @access private
  132.      */
  133.     var $_html_images = array();
  134.  
  135.     /**
  136.      * list of the attachements
  137.      *
  138.      * @var array 
  139.      * @access private
  140.      */
  141.     var $_parts = array();
  142.  
  143.     /**
  144.      * Build parameters
  145.      *
  146.      * @var array 
  147.      * @access private
  148.      */
  149.     var $_build_params = array();
  150.  
  151.     /**
  152.      * Headers for the mail
  153.      *
  154.      * @var array 
  155.      * @access private
  156.      */
  157.     var $_headers = array();
  158.  
  159.     /**
  160.      * End Of Line sequence (for serialize)
  161.      *
  162.      * @var string 
  163.      * @access private
  164.      */
  165.     var $_eol;
  166.  
  167.  
  168.     /**
  169.      * Constructor function.
  170.      *
  171.      * @param string $crlf  what type of linebreak to use.
  172.      *                        Defaults to "\r\n"
  173.      * @return void 
  174.      *
  175.      * @access public
  176.      */
  177.     function Mail_mime($crlf "\r\n")
  178.     {
  179.         $this->_setEOL($crlf);
  180.         $this->_build_params = array(
  181.                                      'head_encoding' => 'quoted-printable',
  182.                                      'text_encoding' => '7bit',
  183.                                      'html_encoding' => 'quoted-printable',
  184.                                      '7bit_wrap'     => 998,
  185.                                      'html_charset'  => 'ISO-8859-1',
  186.                                      'text_charset'  => 'ISO-8859-1',
  187.                                      'head_charset'  => 'ISO-8859-1'
  188.                                     );
  189.     }
  190.  
  191.     /**
  192.      * wakeup function called by unserialize. It re-sets the EOL constant
  193.      *
  194.      * @access private
  195.      */
  196.     function __wakeup()
  197.     {
  198.         $this->_setEOL($this->_eol);
  199.     }
  200.  
  201.  
  202.     /**
  203.      * Accessor function to set the body text. Body text is used if
  204.      * it's not an html mail being sent or else is used to fill the
  205.      * text/plain part that emails clients who don't support
  206.      * html should show.
  207.      *
  208.      * @param  string  $data   Either a string or
  209.      *                           the file name with the contents
  210.      * @param  bool    $isfile If true the first param should be treated
  211.      *                           as a file name, else as a string (default)
  212.      * @param  bool    $append If true the text or file is appended to
  213.      *                           the existing body, else the old body is
  214.      *                           overwritten
  215.      * @return mixed   true on success or PEAR_Error object
  216.      * @access public
  217.      */
  218.     function setTXTBody($data$isfile = false$append = false)
  219.     {
  220.         if (!$isfile{
  221.             if (!$append{
  222.                 $this->_txtbody $data;
  223.             else {
  224.                 $this->_txtbody .= $data;
  225.             }
  226.         else {
  227.             $cont $this->_file2str($data);
  228.             if (PEAR::isError($cont)) {
  229.                 return $cont;
  230.             }
  231.             if (!$append{
  232.                 $this->_txtbody $cont;
  233.             else {
  234.                 $this->_txtbody .= $cont;
  235.             }
  236.         }
  237.         return true;
  238.     }
  239.  
  240.     /**
  241.      * Adds a html part to the mail.
  242.      *
  243.      * @param  string  $data   either a string or the file name with the
  244.      *                           contents
  245.      * @param  bool    $isfile a flag that determines whether $data is a
  246.      *                           filename, or a string(false, default)
  247.      * @return bool    true on success
  248.      * @access public
  249.      */
  250.     function setHTMLBody($data$isfile = false)
  251.     {
  252.         if (!$isfile{
  253.             $this->_htmlbody $data;
  254.         else {
  255.             $cont $this->_file2str($data);
  256.             if (PEAR::isError($cont)) {
  257.                 return $cont;
  258.             }
  259.             $this->_htmlbody $cont;
  260.         }
  261.  
  262.         return true;
  263.     }
  264.  
  265.     /**
  266.      * Adds an image to the list of embedded images.
  267.      *
  268.      * @param  string  $file       the image file name OR image data itself
  269.      * @param  string  $c_type     the content type
  270.      * @param  string  $name       the filename of the image.
  271.      *                               Only use if $file is the image data.
  272.      * @param  bool    $isfile     whether $file is a filename or not.
  273.      *                               Defaults to true
  274.      * @return bool                true on success
  275.      * @access public
  276.      */
  277.     function addHTMLImage($file$c_type='application/octet-stream',
  278.                           $name ''$isfile = true)
  279.     {
  280.         $filedata ($isfile === true$this->_file2str($file)
  281.                                            : $file;
  282.         if ($isfile === true{
  283.             $filename ($name == '' $file $name);
  284.         else {
  285.             $filename $name;
  286.         }
  287.         if (PEAR::isError($filedata)) {
  288.             return $filedata;
  289.         }
  290.         $this->_html_images[= array(
  291.                                       'body'   => $filedata,
  292.                                       'name'   => $filename,
  293.                                       'c_type' => $c_type,
  294.                                       'cid'    => md5(uniqid(time()))
  295.                                      );
  296.         return true;
  297.     }
  298.  
  299.     /**
  300.      * Adds a file to the list of attachments.
  301.      *
  302.      * @param  string  $file        The file name of the file to attach
  303.      *                               OR the file contents itself
  304.      * @param  string  $c_type      The content type
  305.      * @param  string  $name        The filename of the attachment
  306.      *                               Only use if $file is the contents
  307.      * @param  bool    $isfile      Whether $file is a filename or not
  308.      *                               Defaults to true
  309.      * @param  string  $encoding    The type of encoding to use.
  310.      *                               Defaults to base64.
  311.      *                               Possible values: 7bit, 8bit, base64,
  312.      *                               or quoted-printable.
  313.      * @param  string  $disposition The content-disposition of this file
  314.      *                               Defaults to attachment.
  315.      *                               Possible values: attachment, inline.
  316.      * @param  string  $charset     The character set used in the filename
  317.      *                               of this attachment.
  318.      * @param  string  $language    The language of the attachment
  319.      * @param  string  $location    The RFC 2557.4 location of the attachment
  320.      * @return mixed true on success or PEAR_Error object
  321.      * @access public
  322.      */
  323.     function addAttachment($file$c_type 'application/octet-stream',
  324.                            $name ''$isfile = true,
  325.                            $encoding 'base64',
  326.                            $disposition 'attachment',
  327.                            $charset ''$language '',
  328.                            $location '')
  329.     {
  330.         $filedata ($isfile === true$this->_file2str($file)
  331.                                            : $file;
  332.         if ($isfile === true{
  333.             // Force the name the user supplied, otherwise use $file
  334.             $filename (strlen($name)) $name $file;
  335.         else {
  336.             $filename $name;
  337.         }
  338.         if (!strlen($filename)) {
  339.             $err = PEAR::raiseError(
  340.               "The supplied filename for the attachment can't be empty"
  341.             );
  342.             return $err;
  343.         }
  344.         $filename basename($filename);
  345.         if (PEAR::isError($filedata)) {
  346.             return $filedata;
  347.         }
  348.  
  349.         $this->_parts[= array(
  350.                                 'body'        => $filedata,
  351.                                 'name'        => $filename,
  352.                                 'c_type'      => $c_type,
  353.                                 'encoding'    => $encoding,
  354.                                 'charset'     => $charset,
  355.                                 'language'    => $language,
  356.                                 'location'    => $location,
  357.                                 'disposition' => $disposition
  358.                                );
  359.         return true;
  360.     }
  361.  
  362.     /**
  363.      * Get the contents of the given file name as string
  364.      *
  365.      * @param  string  $file_name  path of file to process
  366.      * @return string  contents of $file_name
  367.      * @access private
  368.      */
  369.     function &_file2str($file_name)
  370.     {
  371.         if (!is_readable($file_name)) {
  372.             $err = PEAR::raiseError('File is not readable ' $file_name);
  373.             return $err;
  374.         }
  375.         if (!$fd fopen($file_name'rb')) {
  376.             $err = PEAR::raiseError('Could not open ' $file_name);
  377.             return $err;
  378.         }
  379.         $filesize filesize($file_name);
  380.         if ($filesize == 0){
  381.             $cont =  "";
  382.         }else{
  383.             if ($magic_quote_setting get_magic_quotes_runtime()){
  384.                 set_magic_quotes_runtime(0);
  385.             }
  386.             $cont fread($fd$filesize);
  387.             if ($magic_quote_setting){
  388.                 set_magic_quotes_runtime($magic_quote_setting);
  389.             }
  390.         }
  391.         fclose($fd);
  392.         return $cont;
  393.     }
  394.  
  395.     /**
  396.      * Adds a text subpart to the mimePart object and
  397.      * returns it during the build process.
  398.      *
  399.      * @param mixed    The object to add the part to, or
  400.      *                  null if a new object is to be created.
  401.      * @param string   The text to add.
  402.      * @return object  The text mimePart object
  403.      * @access private
  404.      */
  405.     function &_addTextPart(&$obj$text)
  406.     {
  407.         $params['content_type''text/plain';
  408.         $params['encoding']     $this->_build_params['text_encoding'];
  409.         $params['charset']      $this->_build_params['text_charset'];
  410.         if (is_object($obj)) {
  411.             $ret $obj->addSubpart($text$params);
  412.             return $ret;
  413.         else {
  414.             $ret = new Mail_mimePart($text$params);
  415.             return $ret;
  416.         }
  417.     }
  418.  
  419.     /**
  420.      * Adds a html subpart to the mimePart object and
  421.      * returns it during the build process.
  422.      *
  423.      * @param  mixed   The object to add the part to, or
  424.      *                  null if a new object is to be created.
  425.      * @return object  The html mimePart object
  426.      * @access private
  427.      */
  428.     function &_addHtmlPart(&$obj)
  429.     {
  430.         $params['content_type''text/html';
  431.         $params['encoding']     $this->_build_params['html_encoding'];
  432.         $params['charset']      $this->_build_params['html_charset'];
  433.         if (is_object($obj)) {
  434.             $ret $obj->addSubpart($this->_htmlbody$params);
  435.             return $ret;
  436.         else {
  437.             $ret = new Mail_mimePart($this->_htmlbody$params);
  438.             return $ret;
  439.         }
  440.     }
  441.  
  442.     /**
  443.      * Creates a new mimePart object, using multipart/mixed as
  444.      * the initial content-type and returns it during the
  445.      * build process.
  446.      *
  447.      * @return object  The multipart/mixed mimePart object
  448.      * @access private
  449.      */
  450.     function &_addMixedPart()
  451.     {
  452.         $params['content_type''multipart/mixed';
  453.         $ret = new Mail_mimePart(''$params);
  454.         return $ret;
  455.     }
  456.  
  457.     /**
  458.      * Adds a multipart/alternative part to a mimePart
  459.      * object (or creates one), and returns it during
  460.      * the build process.
  461.      *
  462.      * @param  mixed   The object to add the part to, or
  463.      *                  null if a new object is to be created.
  464.      * @return object  The multipart/mixed mimePart object
  465.      * @access private
  466.      */
  467.     function &_addAlternativePart(&$obj)
  468.     {
  469.         $params['content_type''multipart/alternative';
  470.         if (is_object($obj)) {
  471.             return $obj->addSubpart(''$params);
  472.         else {
  473.             $ret = new Mail_mimePart(''$params);
  474.             return $ret;
  475.         }
  476.     }
  477.  
  478.     /**
  479.      * Adds a multipart/related part to a mimePart
  480.      * object (or creates one), and returns it during
  481.      * the build process.
  482.      *
  483.      * @param mixed    The object to add the part to, or
  484.      *                  null if a new object is to be created
  485.      * @return object  The multipart/mixed mimePart object
  486.      * @access private
  487.      */
  488.     function &_addRelatedPart(&$obj)
  489.     {
  490.         $params['content_type''multipart/related';
  491.         if (is_object($obj)) {
  492.             return $obj->addSubpart(''$params);
  493.         else {
  494.             $ret = new Mail_mimePart(''$params);
  495.             return $ret;
  496.         }
  497.     }
  498.  
  499.     /**
  500.      * Adds an html image subpart to a mimePart object
  501.      * and returns it during the build process.
  502.      *
  503.      * @param  object  The mimePart to add the image to
  504.      * @param  array   The image information
  505.      * @return object  The image mimePart object
  506.      * @access private
  507.      */
  508.     function &_addHtmlImagePart(&$obj$value)
  509.     {
  510.         $params['content_type'$value['c_type'];
  511.         $params['encoding']     'base64';
  512.         $params['disposition']  'inline';
  513.         $params['dfilename']    $value['name'];
  514.         $params['cid']          $value['cid'];
  515.         $ret $obj->addSubpart($value['body']$params);
  516.         return $ret;
  517.     
  518.     }
  519.  
  520.     /**
  521.      * Adds an attachment subpart to a mimePart object
  522.      * and returns it during the build process.
  523.      *
  524.      * @param  object  The mimePart to add the image to
  525.      * @param  array   The attachment information
  526.      * @return object  The image mimePart object
  527.      * @access private
  528.      */
  529.     function &_addAttachmentPart(&$obj$value)
  530.     {
  531.         $params['dfilename']    $value['name'];
  532.         $params['encoding']     $value['encoding'];
  533.         if ($value['charset']{
  534.             $params['charset'$value['charset'];
  535.         }
  536.         if ($value['language']{
  537.             $params['language'$value['language'];
  538.         }
  539.         if ($value['location']{
  540.             $params['location'$value['location'];
  541.         }
  542.         $params['content_type'$value['c_type'];
  543.         $params['disposition']  = isset($value['disposition']
  544.                                   $value['disposition''attachment';
  545.         $ret $obj->addSubpart($value['body']$params);
  546.         return $ret;
  547.     }
  548.  
  549.     /**
  550.      * Returns the complete e-mail, ready to send using an alternative
  551.      * mail delivery method. Note that only the mailpart that is made
  552.      * with Mail_Mime is created. This means that,
  553.      * YOU WILL HAVE NO TO: HEADERS UNLESS YOU SET IT YOURSELF
  554.      * using the $xtra_headers parameter!
  555.      * 
  556.      * @param  string $separation   The separation etween these two parts.
  557.      * @param  array  $build_params The Build parameters passed to the
  558.      *                               &get() function. See &get for more info.
  559.      * @param  array  $xtra_headers The extra headers that should be passed
  560.      *                               to the &headers() function.
  561.      *                               See that function for more info.
  562.      * @param  bool   $overwrite    Overwrite the existing headers with new.
  563.      * @return string The complete e-mail.
  564.      * @access public
  565.      */
  566.     function getMessage($separation = null$build_params = null$xtra_headers = null$overwrite = false)
  567.     {
  568.         if ($separation === null)
  569.         {
  570.             $separation = MAIL_MIME_CRLF;
  571.         }
  572.         $body $this->get($build_params);
  573.         $head $this->txtHeaders($xtra_headers$overwrite);
  574.         $mail $head $separation $body;
  575.         return $mail;
  576.     }
  577.  
  578.  
  579.     /**
  580.      * Builds the multipart message from the list ($this->_parts) and
  581.      * returns the mime content.
  582.      *
  583.      * @param  array  Build parameters that change the way the email
  584.      *                 is built. Should be associative. Can contain:
  585.      *                 head_encoding  -  What encoding to use for the headers.
  586.      *                                   Options: quoted-printable or base64
  587.      *                                   Default is quoted-printable
  588.      *                 text_encoding  -  What encoding to use for plain text
  589.      *                                   Options: 7bit, 8bit, base64, or quoted-printable
  590.      *                                   Default is 7bit
  591.      *                 html_encoding  -  What encoding to use for html
  592.      *                                   Options: 7bit, 8bit, base64, or quoted-printable
  593.      *                                   Default is quoted-printable
  594.      *                 7bit_wrap      -  Number of characters before text is
  595.      *                                   wrapped in 7bit encoding
  596.  &n