thomascube
2007-03-21 ab6f8077e9b719db230798e658ba37f1f386b26b
program/lib/Mail/mime.php
@@ -1,115 +1,176 @@
<?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    |
// | are met:                                                              |
// |                                                                       |
// | 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  |
// |   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>                           |
// |         Tomas V.V.Cox <cox@idecnet.com> (port to PEAR)                |
// +-----------------------------------------------------------------------+
//
// $Id$
/**
 * 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>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * - 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.
 *
 * @category   Mail
 * @package    Mail_Mime
 * @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$
 * @link       http://pear.php.net/package/Mail_mime
 * @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>
 */
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/
 * require PEAR
 *
 * @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>
 * This package depends on PEAR to raise errors.
 */
require_once('PEAR.php');
/**
 * require Mail_mimePart
 *
 * @notes Replaced method _encodeHeaders by the version of ed@avi.ru
 *        See http://pear.php.net/bugs/bug.php?id=30 for details
 * Mail_mimePart contains the code required to
 * create all the different parts a mail can
 * consist of.
 */
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.heyes@heyes-computing.net>
 * @author   Tomas V.V.Cox <cox@idecnet.com>
 * @package  Mail
 * @access   public
 * @category   Mail
 * @package    Mail_Mime
 * @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
 */
class Mail_mime
{
    /**
     * Contains the plain text part of the email
     *
     * @var string
     * @access private
     */
    var $_txtbody;
    /**
     * Contains the html part of the email
     *
     * @var string
     * @access private
     */
    var $_htmlbody;
    /**
     * contains the mime encoded text
     *
     * @var string
     * @access private
     */
    var $_mime;
    /**
     * contains the multipart content
     *
     * @var string
     * @access private
     */
    var $_multipart;
    /**
     * list of the attached images
     *
     * @var array
     * @access private
     */
    var $_html_images = array();
    /**
     * list of the attachements
     *
     * @var array
     * @access private
     */
    var $_parts = array();
    /**
     * Build parameters
     *
     * @var array
     * @access private
     */
    var $_build_params = array();
    /**
     * Headers for the mail
     *
     * @var array
     * @access private
     */
    var $_headers = array();
    /**
     * End Of Line sequence (for serialize)
     *
     * @var string
     * @access private
     */
    var $_eol;
    /**
     * Constructor function
     * Constructor function.
     *
     * @param string $crlf  what type of linebreak to use.
     *                       Defaults to "\r\n"
     * @return void
     *
     * @access public
     */
@@ -117,9 +178,9 @@
    {
        $this->_setEOL($crlf);
        $this->_build_params = array(
                                     'head_encoding' => 'quoted-printable',
                                     'text_encoding' => '7bit',
                                     'html_encoding' => 'quoted-printable',
                                     'header_encoding' => 'quoted-printable',
                                     '7bit_wrap'     => 998,
                                     'html_charset'  => 'ISO-8859-1',
                                     'text_charset'  => 'ISO-8859-1',
@@ -128,7 +189,7 @@
    }
    /**
     * Wakeup (unserialize) - re-sets EOL constant
     * wakeup function called by unserialize. It re-sets the EOL constant
     *
     * @access private
     */
@@ -137,6 +198,7 @@
        $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
@@ -144,12 +206,12 @@
     * html should show.
     *
     * @param  string  $data   Either a string or
     *                         the file name with the contents
     *                          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)
     *                          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
     *                         overwritten
     *                          the existing body, else the old body is
     *                          overwritten
     * @return mixed   true on success or PEAR_Error object
     * @access public
     */
@@ -176,13 +238,13 @@
    }
    /**
     * Adds a html part to the mail
     * Adds a html part to the mail.
     *
     * @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)
     * @return mixed   true on success or PEAR_Error object
     * @param  string  $data   either a string or the file name with the
     *                          contents
     * @param  bool    $isfile a flag that determines whether $data is a
     *                          filename, or a string(false, default)
     * @return bool    true on success
     * @access public
     */
    function setHTMLBody($data, $isfile = false)
@@ -203,24 +265,24 @@
    /**
     * 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
     *                             Defaults to true
     * @return mixed   true on success or PEAR_Error object
     * @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    $isfile     whether $file is a filename or not.
     *                              Defaults to true
     * @return bool                true on success
     * @access public
     */
    function addHTMLImage($file, $c_type='application/octet-stream',
                          $name = '', $isfilename = true)
                          $name = '', $isfile = true)
    {
        $filedata = ($isfilename === true) ? $this->_file2str($file)
        $filedata = ($isfile === true) ? $this->_file2str($file)
                                           : $file;
        if ($isfilename === true) {
            $filename = ($name == '' ? basename($file) : basename($name));
        if ($isfile === true) {
            $filename = ($name == '' ? $file : $name);
        } else {
            $filename = basename($name);
            $filename = $name;
        }
        if (PEAR::isError($filedata)) {
            return $filedata;
@@ -237,32 +299,43 @@
    /**
     * 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
     *                             Defaults to true
     * @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
     *                              Defaults to true
     * @param  string  $encoding    The type of encoding to use.
     *                              Defaults to base64.
     *                              Possible values: 7bit, 8bit, base64,
     *                              or quoted-printable.
     * @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
     *                              of this attachment.
     * @return mixed true on success or PEAR_Error object
     * @access public
     */
    function addAttachment($file, $c_type = 'application/octet-stream',
                           $name = '', $isfilename = true,
                           $encoding = 'base64')
                           $name = '', $isfile = true,
                           $encoding = 'base64',
                           $disposition = 'attachment', $charset = '')
    {
        $filedata = ($isfilename === true) ? $this->_file2str($file)
        $filedata = ($isfile === true) ? $this->_file2str($file)
                                           : $file;
        if ($isfilename === true) {
        if ($isfile === true) {
            // Force the name the user supplied, otherwise use $file
            $filename = (!empty($name)) ? $name : $file;
        } else {
            $filename = $name;
        }
        if (empty($filename)) {
            return PEAR::raiseError(
              'The supplied filename for the attachment can\'t be empty'
            $err = PEAR::raiseError(
              "The supplied filename for the attachment can't be empty"
            );
       return $err;
        }
        $filename = basename($filename);
        if (PEAR::isError($filedata)) {
@@ -270,10 +343,12 @@
        }
        $this->_parts[] = array(
                                'body'     => $filedata,
                                'name'     => $filename,
                                'c_type'   => $c_type,
                                'encoding' => $encoding
                                'body'        => $filedata,
                                'name'        => $filename,
                                'c_type'      => $c_type,
                                'encoding'    => $encoding,
                                'charset'     => $charset,
                                'disposition' => $disposition
                               );
        return true;
    }
@@ -288,16 +363,24 @@
    function &_file2str($file_name)
    {
        if (!is_readable($file_name)) {
            return PEAR::raiseError('File is not readable ' . $file_name);
            $err = PEAR::raiseError('File is not readable ' . $file_name);
            return $err;
        }
        if (!$fd = fopen($file_name, 'rb')) {
            return PEAR::raiseError('Could not open ' . $file_name);
            $err = PEAR::raiseError('Could not open ' . $file_name);
            return $err;
        }
        $filesize = filesize($file_name);
        if ($filesize == 0){
            $cont =  "";
        }else{
            if ($magic_quote_setting = get_magic_quotes_runtime()){
                set_magic_quotes_runtime(0);
            }
            $cont = fread($fd, $filesize);
            if ($magic_quote_setting){
                set_magic_quotes_runtime($magic_quote_setting);
            }
        }
        fclose($fd);
        return $cont;
@@ -319,9 +402,11 @@
        $params['encoding']     = $this->_build_params['text_encoding'];
        $params['charset']      = $this->_build_params['text_charset'];
        if (is_object($obj)) {
            return $obj->addSubpart($text, $params);
            $ret = $obj->addSubpart($text, $params);
            return $ret;
        } else {
            return new Mail_mimePart($text, $params);
            $ret = new Mail_mimePart($text, $params);
            return $ret;
        }
    }
@@ -340,9 +425,11 @@
        $params['encoding']     = $this->_build_params['html_encoding'];
        $params['charset']      = $this->_build_params['html_charset'];
        if (is_object($obj)) {
            return $obj->addSubpart($this->_htmlbody, $params);
            $ret = $obj->addSubpart($this->_htmlbody, $params);
            return $ret;
        } else {
            return new Mail_mimePart($this->_htmlbody, $params);
            $ret = new Mail_mimePart($this->_htmlbody, $params);
            return $ret;
        }
    }
@@ -357,7 +444,8 @@
    function &_addMixedPart()
    {
        $params['content_type'] = 'multipart/mixed';
        return new Mail_mimePart('', $params);
        $ret = new Mail_mimePart('', $params);
        return $ret;
    }
    /**
@@ -376,7 +464,8 @@
        if (is_object($obj)) {
            return $obj->addSubpart('', $params);
        } else {
            return new Mail_mimePart('', $params);
            $ret = new Mail_mimePart('', $params);
            return $ret;
        }
    }
@@ -396,7 +485,8 @@
        if (is_object($obj)) {
            return $obj->addSubpart('', $params);
        } else {
            return new Mail_mimePart('', $params);
            $ret = new Mail_mimePart('', $params);
            return $ret;
        }
    }
@@ -411,12 +501,15 @@
     */
    function &_addHtmlImagePart(&$obj, $value)
    {
        $params['content_type'] = $value['c_type'];
        $params['content_type'] = $value['c_type'] . '; ' .
                                  'name="' . $value['name'] . '"';
        $params['encoding']     = 'base64';
        $params['disposition']  = 'inline';
        $params['dfilename']    = $value['name'];
        $params['cid']          = $value['cid'];
        $obj->addSubpart($value['body'], $params);
        $ret = $obj->addSubpart($value['body'], $params);
        return $ret;
    }
    /**
@@ -430,12 +523,53 @@
     */
    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);
        $params['encoding']     = $value['encoding'];
        if ($value['disposition'] != "inline") {
            $fname = array("fname" => $value['name']);
            $fname_enc = $this->_encodeHeaders($fname, array('head_charset' => $value['charset'] ? $value['charset'] : 'iso-8859-1'));
            $params['dfilename'] = $fname_enc['fname'];
        }
        if ($value['charset']) {
            $params['charset'] = $value['charset'];
        }
        $params['content_type'] = $value['c_type'] . '; ' .
                                  'name="' . $params['dfilename'] . '"';
        $params['disposition']  = isset($value['disposition']) ?
                                  $value['disposition'] : 'attachment';
        $ret = $obj->addSubpart($value['body'], $params);
        return $ret;
    }
    /**
     * 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.
     * @access public
     */
    function getMessage($separation = null, $build_params = null, $xtra_headers = null, $overwrite = false)
    {
        if ($separation === null)
        {
            $separation = MAIL_MIME_CRLF;
        }
        $body = $this->get($build_params);
        $head = $this->txtHeaders($xtra_headers, $overwrite);
        $mail = $head . $separation . $body;
        return $mail;
    }
    /**
     * Builds the multipart message from the list ($this->_parts) and
@@ -443,9 +577,14 @@
     *
     * @param  array  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
     *                                  Options: 7bit, 8bit, base64, or quoted-printable
     *                                  Default is 7bit
     *                html_encoding  -  What encoding to use for html
     *                                  Options: 7bit, 8bit, base64, or quoted-printable
     *                                  Default is quoted-printable
     *                7bit_wrap      -  Number of characters before text is
     *                                  wrapped in 7bit encoding
@@ -468,13 +607,19 @@
        }
        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';
            foreach ($this->_html_images as $key => $value) {
                $regex = array();
                $regex[] = '#(\s)((?i)src|background|href(?-i))\s*=\s*(["\']?)' .
                            preg_quote($value['name'], '#') . '\3#';
                $regex[] = '#(?i)url(?-i)\(\s*(["\']?)' .
                            preg_quote($value['name'], '#') . '\1\s*\)#';
                $rep = array();
                $rep[] = '\1\2=\3cid:' . $value['cid'] .'\3';
                $rep[] = 'url(\1cid:' . $value['cid'] . '\2)';
                $this->_htmlbody = preg_replace($regex, $rep,
                                       $this->_htmlbody
                                   );
                $this->_html_images[$key]['name'] = basename($this->_html_images[$key]['name']);
            }
        }
@@ -567,10 +712,12 @@
            $output = $message->encode();
            $this->_headers = array_merge($this->_headers,
                                          $output['headers']);
            return $output['body'];
            $body = $output['body'];
            return $body;
        } else {
            return false;
            $ret = false;
            return $ret;
        }
    }
@@ -581,10 +728,11 @@
     *
     * @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)
    function &headers($xtra_headers = null, $overwrite = false)
    {
        // Content-Type header should already be present,
        // So just add mime version header
@@ -592,9 +740,14 @@
        if (isset($xtra_headers)) {
            $headers = array_merge($headers, $xtra_headers);
        }
        $this->_headers = array_merge($headers, $this->_headers);
        if ($overwrite){
            $this->_headers = array_merge($this->_headers, $headers);
        }else{
            $this->_headers = array_merge($headers, $this->_headers);
        }
        return $this->_encodeHeaders($this->_headers);
        $encodedHeaders = $this->_encodeHeaders($this->_headers);
        return $encodedHeaders;
    }
    /**
@@ -603,12 +756,13 @@
     *
     * @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)
    function txtHeaders($xtra_headers = null, $overwrite = false)
    {
        $headers = $this->headers($xtra_headers);
        $headers = $this->headers($xtra_headers, $overwrite);
        $ret = '';
        foreach ($headers as $key => $val) {
            $ret .= "$key: $val" . MAIL_MIME_CRLF;
@@ -671,40 +825,164 @@
    }
    /**
    * Encodes a header as per RFC2047
    *
    * @param  string  $input The header data to encode
    * @return string         Encoded data
    * @access private
    */
    function _encodeHeaders($input)
     * 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)
    {
        foreach ($input as $hdr_name => $hdr_value) {
            preg_match_all('/([\w\-]*[\x80-\xFF]+[\w\-]*(\s+[\w\-]*[\x80-\xFF]+[\w\-]*)*)\s*/',
            $hdr_value, $matches);
            foreach ($matches[1] as $value) {
                switch ($head_encoding = $this->_build_params['head_encoding']) {
                case 'base64':
                    $symbol = 'B';
                    $replacement = base64_encode($value);
                    break;
        $input = array("To" => $recipients);
        $retval = $this->_encodeHeaders($input);
        return $retval["To"] ;
    }
                default:
                    if ($head_encoding != 'quoted-printable') {
                        PEAR::raiseError('Invalid header encoding specified; using `quoted-printable` instead',
                                        NULL,
                                        PEAR_ERROR_TRIGGER,
                                        E_USER_WARNING);
                    }
                    $symbol = 'Q';
                    $replacement = preg_replace('/([\s_=\?\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
                }
                $hdr_value = str_replace($value, '=?' . $this->_build_params['head_charset'] . '?' . $symbol . '?' . $replacement . '?=', $hdr_value);
            }
            $input[$hdr_name] = $hdr_value;
    /**
     * 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;
    }
@@ -725,4 +1003,3 @@
    
} // End of class
?>