Source for file class.mimePart.php

Documentation is available at class.mimePart.php

  1. <?php
  2.  
  3. /**
  4.  *
  5.  * @access public
  6.  * @author  Richard Heyes <richard@phpguru.org>
  7.  * @copyright Copyright (c) 2005
  8.  * @version $Revision: 1.3 $
  9.  * @package src
  10.  * @subpackage mail
  11.  */
  12.  
  13.  
  14. /**
  15. * This file is part of the htmlMimeMail5 package (http://www.phpguru.org/)
  16. *
  17. * htmlMimeMail5 is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * htmlMimeMail5 is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with htmlMimeMail5; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  30. *
  31. * C Copyright 2005 Richard Heyes
  32. */
  33.  
  34. /**
  35.  *
  36.  *  Raw mime encoding class
  37.  *
  38.  * What is it?
  39.  *   This class enables you to manipulate and build
  40.  *   a mime email from the ground up.
  41.  *
  42.  * Why use this instead of mime.php?
  43.  *   mime.php is a userfriendly api to this class for
  44.  *   people who aren't interested in the internals of
  45.  *   mime mail. This class however allows full control
  46.  *   over the email.
  47.  *
  48.  * Eg.
  49.  *
  50.  * // Since multipart/mixed has no real body, (the body is
  51.  * // the subpart), we set the body argument to blank.
  52.  *
  53.  * $params['content_type'] = 'multipart/mixed';
  54.  * $email = new src_mail_MIMEPart('', $params);
  55.  *
  56.  * // Here we add a text part to the multipart we have
  57.  * // already. Assume $body contains plain text.
  58.  *
  59.  * $params['content_type'] = 'text/plain';
  60.  * $params['encoding']     = '7bit';
  61.  * $text = $email->addSubPart($body, $params);
  62.  *
  63.  * // Now add an attachment. Assume $attach is
  64.  * the contents of the attachment
  65.  *
  66.  * $params['content_type'] = 'application/zip';
  67.  * $params['encoding']     = 'base64';
  68.  * $params['disposition']  = 'attachment';
  69.  * $params['dfilename']    = 'example.zip';
  70.  * $attach =& $email->addSubPart($body, $params);
  71.  *
  72.  * // Now build the email. Note that the encode
  73.  * // function returns an associative array containing two
  74.  * // elements, body and headers. You will need to add extra
  75.  * // headers, (eg. Mime-Version) before sending.
  76.  *
  77.  * $email = $message->encode();
  78.  * $email['headers'][] = 'Mime-Version: 1.0';
  79.  *
  80.  *
  81.  * Further examples are available at http://www.phpguru.org
  82.  *
  83.  * TODO:
  84.  *  - Set encode() to return the $obj->encoded if encode()
  85.  *    has already been run. Unless a flag is passed to specifically
  86.  *    re-build the message.
  87.  *
  88.  * Short description of class src_foundation_admin_ADMINARCHIV
  89.  *
  90.  * @access public
  91.  * @author  Richard Heyes <richard@phpguru.org>
  92.  * @copyright Copyright (c) 2005
  93.  * @version $Revision: 1.3 $
  94.  * @package src
  95.  * @subpackage mail
  96.  */
  97.    /**
  98.    * The encoding type of this part
  99.    * @var string 
  100.    */
  101.    private $encoding;
  102.  
  103.    /**
  104.    * An array of subparts
  105.    * @var array 
  106.    */
  107.    private $subparts;
  108.  
  109.    /**
  110.    * The output of this part after being built
  111.    * @var string 
  112.    */
  113.    private $encoded;
  114.  
  115.    /**
  116.    * Headers for this part
  117.    * @var array 
  118.    */
  119.    private $headers;
  120.  
  121.    /**
  122.    * The body of this part (not encoded)
  123.    * @var string 
  124.    */
  125.    private $body;
  126.  
  127.    /**
  128.    * Constructor.
  129.    *
  130.    * Sets up the object.
  131.    *
  132.    * @param $body   - The body of the mime part if any.
  133.    * @param $params - An associative array of parameters:
  134.    *                   content_type - The content type for this part eg multipart/mixed
  135.    *                   encoding     - The encoding to use, 7bit, 8bit, base64, or quoted-printable
  136.    *                   cid          - Content ID to apply
  137.    *                   disposition  - Content disposition, inline or attachment
  138.    *                   dfilename    - Optional filename parameter for content disposition
  139.    *                   description  - Content description
  140.    *                   charset      - Character set to use
  141.    * @access public
  142.    */
  143.    public function __construct($body ''$params array ()) {
  144.       if (!defined('MAIL_MIMEPART_CRLF')) {
  145.          define('MAIL_MIMEPART_CRLF'defined('MAIL_MIME_CRLF'MAIL_MIME_CRLF "\r\n"true);
  146.       }
  147.  
  148.       foreach ($params as $key => $value{
  149.          switch ($key{
  150.             case 'content_type' :
  151.                $headers['Content-Type'$value (isset ($charset'; charset="' $charset '"' '');
  152.                break;
  153.  
  154.             case 'encoding' :
  155.                $this->encoding $value;
  156.                $headers['Content-Transfer-Encoding'$value;
  157.                break;
  158.  
  159.             case 'cid' :
  160.                $headers['Content-ID''<' $value '>';
  161.                break;
  162.  
  163.             case 'disposition' :
  164.                $headers['Content-Disposition'$value (isset ($dfilename'; filename="' $dfilename '"' '');
  165.                break;
  166.  
  167.             case 'dfilename' :
  168.                if (isset ($headers['Content-Disposition'])) {
  169.                   $headers['Content-Disposition'.= '; filename="' $value '"';
  170.                else {
  171.                   $dfilename $value;
  172.                }
  173.                break;
  174.  
  175.             case 'description' :
  176.                $headers['Content-Description'$value;
  177.                break;
  178.  
  179.             case 'charset' :
  180.                if (isset ($headers['Content-Type'])) {
  181.                   $headers['Content-Type'.= '; charset="' $value '"';
  182.                else {
  183.                   $charset $value;
  184.                }
  185.                break;
  186.          }
  187.       }
  188.  
  189.       // Default content-type
  190.       if (!isset ($headers['Content-Type'])) {
  191.          $headers['Content-Type''text/plain';
  192.       }
  193.  
  194.       // Default encoding
  195.       if (!isset ($this->encoding)) {
  196.          $this->encoding '7bit';
  197.       }
  198.  
  199.       // Assign stuff to member variables
  200.       $this->encoded array ();
  201.       $this->headers $headers;
  202.       $this->body $body;
  203.    }
  204.  
  205.    /**
  206.    * Encodes and returns the email. Also stores
  207.    * it in the encoded member variable
  208.    *
  209.    * @return An associative array containing two elements,
  210.    *          body and headers. The headers element is itself
  211.    *          an indexed array.
  212.    */
  213.    public function encode({
  214.       $encoded $this->encoded;
  215.  
  216.       if (!empty ($this->subparts)) {
  217.          srand((double) microtime(1000000);
  218.          $boundary '=_' md5(uniqid(rand()) microtime());
  219.          $this->headers['Content-Type'.= ';' MAIL_MIMEPART_CRLF "\t" 'boundary="' $boundary '"';
  220.  
  221.          // Add body parts to $subparts
  222.          for ($i 0$i count($this->subparts)$i++{
  223.             $headers array ();
  224.             $tmp $this->subparts[$i]->encode();
  225.             foreach ($tmp['headers'as $key => $value{
  226.                $headers[$key ': ' $value;
  227.             }
  228.             $subparts[implode(MAIL_MIMEPART_CRLF$headersMAIL_MIMEPART_CRLF MAIL_MIMEPART_CRLF $tmp['body'];
  229.          }
  230.  
  231.          $encoded['body''--' $boundary MAIL_MIMEPART_CRLF .
  232.          implode('--' $boundary MAIL_MIMEPART_CRLF$subparts.
  233.          '--' $boundary '--' MAIL_MIMEPART_CRLF;
  234.       else {
  235.          $encoded['body'$this->getEncodedData($this->body$this->encodingMAIL_MIMEPART_CRLF;
  236.       }
  237.  
  238.       // Add headers to $encoded
  239.       $encoded['headers'$this->headers;
  240.  
  241.       return $encoded;
  242.    }
  243.  
  244.    /**
  245.    * Adds a subpart to current mime part and returns
  246.    * a reference to it
  247.    *
  248.    * @param $body   The body of the subpart, if any.
  249.    * @param $params The parameters for the subpart, same
  250.    *                 as the $params argument for constructor.
  251.    * @return reference to the part you just added.
  252.    */
  253.    public function addSubPart($body$params{
  254.       $this->subparts[new src_mail_MIMEPart($body$params);
  255.  
  256.       return $this->subparts[count($this->subparts1];
  257.    }
  258.  
  259.    /**
  260.    * Returns encoded data based upon encoding passed to it
  261.    *
  262.    * @param $data     The data to encode.
  263.    * @param $encoding The encoding type to use, 7bit, base64,
  264.    *                   or quoted-printable.
  265.    */
  266.    private function getEncodedData($data$encoding{
  267.       switch ($encoding{
  268.          case '8bit' :
  269.          case '7bit' :
  270.             return $data;
  271.  
  272.          case 'quoted-printable' :
  273.             return $this->quotedPrintableEncode($data);
  274.  
  275.          case 'base64' :
  276.             return rtrim(chunk_split(base64_encode($data)76MAIL_MIMEPART_CRLF));
  277.  
  278.          default :
  279.             return $data;
  280.       }
  281.    }
  282.  
  283.    /**
  284.    * Encodes data to quoted-printable standard.
  285.    *
  286.    * @param $input    The data to encode
  287.    * @param $line_max Optional max line length. Should
  288.    *                   not be more than 76 chars
  289.    */
  290.    private function quotedPrintableEncode($input$line_max 76{
  291.       $lines preg_split("/\r?\n/"$input);
  292.       $eol MAIL_MIMEPART_CRLF;
  293.       $escape '=';
  294.       $output '';
  295.  
  296.       while (list ($lineeach($lines)) {
  297.  
  298.          $linlen strlen($line);
  299.          $newline '';
  300.  
  301.          for ($i 0$i $linlen$i++{
  302.             $char substr($line$i1);
  303.             $dec ord($char);
  304.  
  305.             if (($dec == 32AND ($i == ($linlen -1))) // convert space at eol only
  306.                $char '=20';
  307.  
  308.             }
  309.             elseif ($dec == 9{
  310.                // Do nothing if a tab.
  311.             }
  312.             elseif (($dec == 61OR ($dec 32OR ($dec 126)) {
  313.                $char $escape strtoupper(sprintf('%02s'dechex($dec)));
  314.             }
  315.  
  316.             if ((strlen($newlinestrlen($char)) >= $line_max// MAIL_MIMEPART_CRLF is not counted
  317.                $output .= $newline $escape $eol// soft line break; " =\r\n" is okay
  318.                $newline '';
  319.             }
  320.             $newline .= $char;
  321.          // end of for
  322.          $output .= $newline $eol;
  323.       }
  324.       $output substr($output0-strlen($eol))// Don't want last crlf
  325.       return $output;
  326.    }
  327. // End of class
  328. ?>

Documentation generated on Sat, 24 Mar 2007 09:59:39 +0100 by phpDocumentor 1.3.1