Source for file class.htmlMimeMail5.php

Documentation is available at class.htmlMimeMail5.php

  1. <?php
  2.  
  3.  
  4. /**
  5. * This file is part of the htmlMimeMail5 package (http://www.phpguru.org/)
  6. *
  7. * htmlMimeMail5 is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * htmlMimeMail5 is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with htmlMimeMail5; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20. *
  21.  *
  22.  * @author  Richard Heyes <richard@phpguru.org>
  23.  * @version $Revision: 1.3 $
  24.  * @package src
  25.  * @subpackage mail
  26.  * @copyright Copyright (c) 2005
  27.  * @access public
  28.  ***/
  29.    /**
  30.    * The html part of the message
  31.    * @var string 
  32.    */
  33.    private $html;
  34.  
  35.    /**
  36.    * The text part of the message(only used in TEXT only messages)
  37.    * @private string
  38.    */
  39.    private $text;
  40.  
  41.    /**
  42.    * The main body of the message after building
  43.    * @private string
  44.    */
  45.    private $output;
  46.  
  47.    /**
  48.    * An array of embedded images   /objects
  49.    * @private array
  50.    */
  51.    private $html_images;
  52.  
  53.    /**
  54.    * An array of recognised image types for the findHtmlImages() method
  55.    * @private array
  56.    */
  57.    private $image_types;
  58.  
  59.    /**
  60.    * Parameters that affect the build process
  61.    * @private array
  62.    */
  63.    private $build_params;
  64.  
  65.    /**
  66.    * Array of attachments
  67.    * @private array
  68.    */
  69.    private $attachments;
  70.  
  71.    /**
  72.    * The main message headers
  73.    * @private array
  74.    */
  75.    private $headers;
  76.  
  77.    /**
  78.    * Whether the message has been built or not
  79.    * @private boolean
  80.    */
  81.    private $is_built;
  82.  
  83.    /**
  84.    * The return path address. If not set the From:
  85.    * address is used instead
  86.    * @private string
  87.    */
  88.    private $return_path;
  89.  
  90.    /**
  91.    * Array of information needed for smtp sending
  92.    * @private array
  93.    */
  94.    private $smtp_params;
  95.  
  96.    /**
  97.    * Sendmail path. Do not include -f
  98.    * @private $sendmail_path
  99.    */
  100.    private $sendmail_path;
  101.  
  102.    /**
  103.    * Constructor function.
  104.    */
  105.    public function __construct({
  106.       /**
  107.       * Initialise some variables.
  108.       */
  109.       $this->attachments array ();
  110.       $this->html_images array ();
  111.       $this->headers array ();
  112.       $this->is_built false;
  113.       $this->text '';
  114.       $this->sendmail_path '/usr/lib/sendmail -ti';
  115.  
  116.       /**
  117.       * If you want the auto load functionality
  118.       * to find other image/file types, add the
  119.       * extension and content type here.
  120.       */
  121.       $this->image_types array (
  122.          'gif' => 'image/gif',
  123.          'jpg' => 'image/jpeg',
  124.          'jpeg' => 'image/jpeg',
  125.          'jpe' => 'image/jpeg',
  126.          'bmp' => 'image/bmp',
  127.          'png' => 'image/png',
  128.          'tif' => 'image/tiff',
  129.          'tiff' => 'image/tiff',
  130.          'swf' => 'application/x-shockwave-flash'
  131.       );
  132.  
  133.       /**
  134.       * Set these up
  135.       */
  136.       $this->build_params['html_encoding'new QPrintEncoding();
  137.       $this->build_params['text_encoding'new SevenBitEncoding();
  138.       $this->build_params['html_charset''ISO-8859-1';
  139.       $this->build_params['text_charset''ISO-8859-1';
  140.       $this->build_params['head_charset''ISO-8859-1';
  141.       $this->build_params['text_wrap'998;
  142.  
  143.       /**
  144.       * Defaults for smtp sending
  145.       */
  146.       if (!empty ($_SERVER['HTTP_HOST'])) {
  147.          $helo $_SERVER['HTTP_HOST'];
  148.  
  149.       }
  150.       elseif (!empty ($_SERVER['SERVER_NAME'])) {
  151.          $helo $_SERVER['SERVER_NAME'];
  152.  
  153.       else {
  154.          $helo 'localhost';
  155.       }
  156.  
  157.       $this->smtp_params['host''localhost';
  158.       $this->smtp_params['port'25;
  159.       $this->smtp_params['helo'$helo;
  160.       $this->smtp_params['auth'false;
  161.       $this->smtp_params['user''';
  162.       $this->smtp_params['pass''';
  163.  
  164.       /**
  165.       * Make sure the MIME version header is first.
  166.       */
  167.       $this->headers['MIME-Version''1.0';
  168.       $this->headers['X-Mailer''htmlMimeMail5 <http://www.phpguru.org/>';
  169.    }
  170.  
  171.    /**
  172.    * Accessor to set the CRLF style
  173.    *
  174.    * @param string $crlf CRLF style to use.
  175.    *                      Use \r\n for SMTP, and \n
  176.    *                      for normal.
  177.    */
  178.    public function setCRLF($crlf "\n"{
  179.       if (!defined('CRLF')) {
  180.          define('CRLF'$crlftrue);
  181.       }
  182.  
  183.       if (!defined('MAIL_MIMEPART_CRLF')) {
  184.          define('MAIL_MIMEPART_CRLF'$crlftrue);
  185.       }
  186.    }
  187.  
  188.    /**
  189.    * Accessor to set the SMTP parameters
  190.    *
  191.    * @param string $host Hostname
  192.    * @param string $port Port
  193.    * @param string $helo HELO string to use
  194.    * @param bool   $auth User authentication or not
  195.    * @param string $user Username
  196.    * @param string $pass Password
  197.    */
  198.    public function setSMTPParams($host null$port null$helo null$auth null$user null$pass null{
  199.       if (!is_null($host))
  200.          $this->smtp_params['host'$host;
  201.       if (!is_null($port))
  202.          $this->smtp_params['port'$port;
  203.       if (!is_null($helo))
  204.          $this->smtp_params['helo'$helo;
  205.       if (!is_null($auth))
  206.          $this->smtp_params['auth'$auth;
  207.       if (!is_null($user))
  208.          $this->smtp_params['user'$user;
  209.       if (!is_null($pass))
  210.          $this->smtp_params['pass'$pass;
  211.    }
  212.  
  213.    /**
  214.    * Sets sendmail path and options (optionally) (when directly piping to sendmail)
  215.    *
  216.    * @param string $path Path and options for sendmail command
  217.    */
  218.    public function setSendmailPath($path{
  219.       $this->sendmail_path $path;
  220.    }
  221.  
  222.    /**
  223.    * Accessor function to set the text encoding
  224.    *
  225.    * @param object $encoding Text encoding to use
  226.    */
  227.    public function setTextEncoding(iEncoding $encoding{
  228.       $this->build_params['text_encoding'$encoding;
  229.    }
  230.  
  231.    /**
  232.    * Accessor function to set the HTML encoding
  233.    *
  234.    * @param object $encoding HTML encoding to use
  235.    */
  236.    public function setHTMLEncoding(iEncoding $encoding{
  237.       $this->build_params['html_encoding'$encoding;
  238.    }
  239.  
  240.    /**
  241.    * Accessor function to set the text charset
  242.    *
  243.    * @param string $charset Character set to use
  244.    */
  245.    public function setTextCharset($charset 'ISO-8859-1'{
  246.       $this->build_params['text_charset'$charset;
  247.    }
  248.  
  249.    /**
  250.    * Accessor function to set the HTML charset
  251.    *
  252.    * @param string $charset Character set to use
  253.    */
  254.    public function setHTMLCharset($charset 'ISO-8859-1'{
  255.       $this->build_params['html_charset'$charset;
  256.    }
  257.  
  258.    /**
  259.    * Accessor function to set the header encoding charset
  260.    *
  261.    * @param string $charset Character set to use
  262.    */
  263.    public function setHeadCharset($charset 'ISO-8859-1'{
  264.       $this->build_params['head_charset'$charset;
  265.    }
  266.  
  267.    /**
  268.    * Accessor function to set the text wrap count
  269.    *
  270.    * @param integer $count Point at which to wrap text
  271.    */
  272.    public function setTextWrap($count 998{
  273.       $this->build_params['text_wrap'$count;
  274.    }
  275.  
  276.    /**
  277.    * Accessor to set a header
  278.    *
  279.    * @param string $name  Name of header
  280.    * @param string $value Value of header
  281.    */
  282.    public function setHeader($name$value{
  283.       $this->headers[$name$value;
  284.    }
  285.  
  286.    /**
  287.    * Accessor to add a Subject: header
  288.    *
  289.    * @param string $subject Subject to set
  290.    */
  291.    public function setSubject($subject{
  292.       $this->headers['Subject'$subject;
  293.    }
  294.  
  295.    /**
  296.    * Accessor to add a From: header
  297.    *
  298.    * @param string $from From address
  299.    */
  300.    public function setFrom($from{
  301.       $this->headers['From'$from;
  302.    }
  303.  
  304.    /**
  305.    * Accessor to set priority. Priority given should be either
  306.    * high, normal or low. Can also be specified numerically,
  307.    * being 1, 3 or 5 (respectively).
  308.    *
  309.    * @param mixed $priority The priority to use.
  310.    */
  311.    public function setPriority($priority 'normal'{
  312.       switch (strtolower($priority)) {
  313.          case 'high' :
  314.          case '1' :
  315.             $this->headers['X-Priority''1';
  316.             $this->headers['X-MSMail-Priority''High';
  317.             break;
  318.  
  319.          case 'normal' :
  320.          case '3' :
  321.             $this->headers['X-Priority''3';
  322.             $this->headers['X-MSMail-Priority''Normal';
  323.             break;
  324.  
  325.          case 'low' :
  326.          case '5' :
  327.             $this->headers['X-Priority''5';
  328.             $this->headers['X-MSMail-Priority''Low';
  329.             break;
  330.       }
  331.    }
  332.  
  333.    /**
  334.    * Accessor to set the return path
  335.    *
  336.    * @param string $return_path Return path to use
  337.    */
  338.    public function setReturnPath($return_path{
  339.       $this->return_path $return_path;
  340.    }
  341.  
  342.    /**
  343.    * Accessor to add a Cc: header
  344.    *
  345.    * @param string $cc Carbon Copy address
  346.    */
  347.    public function setCc($cc{
  348.       $this->headers['Cc'$cc;
  349.    }
  350.  
  351.    /**
  352.    * Accessor to add a Bcc: header
  353.    *
  354.    * @param string $bcc Blind Carbon Copy address
  355.    */
  356.    public function setBcc($bcc{
  357.       $this->headers['Bcc'$bcc;
  358.    }
  359.  
  360.    /**
  361.    * Adds plain text. Use this function
  362.    * when NOT sending html email
  363.    *
  364.    * @param string $text Plain text of email
  365.    */
  366.    public function setText($text{
  367.       $this->text $text;
  368.    }
  369.  
  370.    /**
  371.    * Adds HTML to the emails, with an associated text part.
  372.    * If third part is given, images in the email will be loaded
  373.    * from this directory.
  374.    *
  375.    * @param string $html       HTML part of email
  376.    * @param string $images_dir Images directory
  377.    */
  378.    function setHTML($html$images_dir null{
  379.       $this->html $html;
  380.  
  381.       if (!empty ($images_dir)) {
  382.          $this->findHtmlImages($images_dir);
  383.       }
  384.    }
  385.  
  386.    /**
  387.    * Function for extracting images from
  388.    * html source. This function will look
  389.    * through the html code supplied by setHTML()
  390.    * and find any file that ends in one of the
  391.    * extensions defined in $obj->image_types.
  392.    * If the file exists it will read it in and
  393.    * embed it, (not an attachment).
  394.    *
  395.    * @param string $images_dir Images directory to look in
  396.    */
  397.    private function findHtmlImages($images_dir{
  398.       // Build the list of image extensions
  399.       $extensions array_keys($this->image_types);
  400.  
  401.       preg_match_all('/(?:"|\')([^"\']+\.(' implode('|'$extensions'))(?:"|\')/Ui'$this->html$matches);
  402.  
  403.       foreach ($matches[1as $m{
  404.          if (file_exists($images_dir $m)) {
  405.             $html_images[$m;
  406.             $this->html str_replace($mbasename($m)$this->html);
  407.          }
  408.       }
  409.  
  410.       /**
  411.       * Go thru found images
  412.       */
  413.       if (!empty ($html_images)) {
  414.  
  415.          // If duplicate images are embedded, they may show up as attachments, so remove them.
  416.          $html_images array_unique($html_images);
  417.          sort($html_images);
  418.  
  419.          foreach ($html_images as $img{
  420.             if ($image file_get_contents($images_dir $img)) {
  421.                $ext preg_replace('#^.*\.(\w{3,4})$#e''strtolower("$1")'$img);
  422.                $content_type $this->image_types[$ext];
  423.                $this->addEmbeddedImage(new stringEmbeddedImage($imagebasename($img)$content_type));
  424.             }
  425.          }
  426.       }
  427.    }
  428.  
  429.    /**
  430.    * Adds an image to the list of embedded
  431.    * images.
  432.    *
  433.    * @param string $object Embedded image object
  434.    */
  435.    public function addEmbeddedImage($embeddedImage{
  436.       $embeddedImage->cid md5(uniqid(time()));
  437.  
  438.       $this->html_images[$embeddedImage;
  439.    }
  440.  
  441.    /**
  442.    * Adds a file to the list of attachments.
  443.    *
  444.    * @param string $attachment Attachment object
  445.    */
  446.    public function addAttachment($attachment{
  447.       $this->attachments[$attachment;
  448.    }
  449.  
  450.    /**
  451.    * Adds a text subpart to a mime_part object
  452.    *
  453.    * @param  object $obj 
  454.    * @return object      Mime part object
  455.    */
  456.    private function addTextPart($message{
  457.       $params['content_type''text/plain';
  458.       $params['encoding'$this->build_params['text_encoding']->getType();
  459.       $params['charset'$this->build_params['text_charset'];
  460.  
  461.       if (!empty ($message)) {
  462.          $message->addSubpart($this->text$params);
  463.       else {
  464.          $message new src_mail_mimePart($this->text$params);
  465.       }
  466.    }
  467.  
  468.    /**
  469.    * Adds a html subpart to a mime_part object
  470.    *
  471.    * @param object $obj 
  472.    * @return object     Mime part object
  473.    */
  474.    private function addHtmlPart($message{
  475.       $params['content_type''text/html';
  476.       $params['encoding'$this->build_params['html_encoding']->getType();
  477.       $params['charset'$this->build_params['html_charset'];
  478.  
  479.       if (!empty ($message)) {
  480.          $message->addSubpart($this->html$params);
  481.       else {
  482.          $message new src_mail_mimePart($this->html$params);
  483.       }
  484.    }
  485.  
  486.    /**
  487.    * Starts a message with a mixed part
  488.    *
  489.    * @return object Mime part object
  490.    */
  491.    private function addMixedPart($message{
  492.       $params['content_type''multipart/mixed';
  493.  
  494.       $message new src_mail_mimePart(''$params);
  495.    }
  496.  
  497.    /**
  498.    * Adds an alternative part to a mime_part object
  499.    *
  500.    * @param  object $obj 
  501.    * @return object      Mime part object
  502.    */
  503.    private function addAlternativePart($message{
  504.       $params['content_type''multipart/alternative';
  505.  
  506.       if (!empty ($message)) {
  507.          return $message->addSubpart(''$params);
  508.       else {
  509.          $message new src_mail_mimePart(''$params);
  510.       }
  511.    }
  512.  
  513.    /**
  514.    * Adds a html subpart to a mime_part object
  515.    *
  516.    * @param  object $obj 
  517.    * @return object      Mime part object
  518.    */
  519.    private function addRelatedPart($message{
  520.       $params['content_type''multipart/related';
  521.  
  522.       if (!empty ($message)) {
  523.          return $message->addSubpart(''$params);
  524.       else {
  525.          $message new src_mail_mimePart(''$params);
  526.       }
  527.    }
  528.  
  529.    /**
  530.    * Adds all html images to a mime_part object
  531.    *
  532.    * @param  object $obj Message object
  533.    */
  534.    private function addHtmlImageParts($message{
  535.       foreach ($this->html_images as $value{
  536.          $params['content_type'$value->contentType;
  537.          $params['encoding'$value->encoding->getType();
  538.          $params['disposition''inline';
  539.          $params['dfilename'$value->name;
  540.          $params['cid'$value->cid;
  541.  
  542.          $message->addSubpart($value->data$params);
  543.       }
  544.    }
  545.  
  546.    /**
  547.    * Adds all attachments to a mime_part object
  548.    *
  549.    * @param object $obj Message object
  550.    */
  551.    private function addAttachmentParts($message{
  552.       foreach ($this->attachments as $value{
  553.          $params['content_type'$value->contentType;
  554.          $params['encoding'$value->encoding->getType();
  555.          $params['disposition''attachment';
  556.          $params['dfilename'$value->name;
  557.  
  558.          $message->addSubpart($value->data$params);
  559.       }
  560.    }
  561.  
  562.    /**
  563.    * Builds the multipart message.
  564.    */
  565.    private function build({
  566.       if (!empty ($this->html_images)) {
  567.          foreach ($this->html_images as $value{
  568.             $quoted preg_quote($value->name);
  569.             $cid preg_quote($value->cid);
  570.  
  571.             $this->html preg_replace("#src=\"$quoted\"|src='$quoted'#""src=\"cid:$cid\""$this->html);
  572.             $this->html preg_replace("#background=\"$quoted\"|background='$quoted'#""background=\"cid:$cid\""$this->html);
  573.          }
  574.       }
  575.  
  576.       $message null;
  577.       $attachments !empty ($this->attachments);
  578.       $html_images !empty ($this->html_images);
  579.       $html !empty ($this->html);
  580.       $text !$html;
  581.  
  582.       switch (true{
  583.          case $text :
  584.             $message null;
  585.             if ($attachments{
  586.                $this->addMixedPart($message);
  587.             }
  588.  
  589.             $this->addTextPart($message);
  590.  
  591.             // Attachments
  592.             $this->addAttachmentParts($message);
  593.             break;
  594.  
  595.          case $html AND !$attachments AND !$html_images :
  596.             $this->addAlternativePart($message);
  597.  
  598.             $this->addTextPart($message);
  599.             $this->addHtmlPart($message);
  600.             break;
  601.  
  602.          case $html AND !$attachments AND $html_images :
  603.             $this->addRelatedPart($message);
  604.             $alt $this->addAlternativePart($message);
  605.  
  606.             $this->addTextPart($alt);
  607.             $this->addHtmlPart($alt);
  608.  
  609.             // HTML images
  610.             $this->addHtmlImageParts($message);
  611.             break;
  612.  
  613.          case $html AND $attachments AND !$html_images :
  614.             $this->addMixedPart($message);
  615.             $alt $this->addAlternativePart($message);
  616.  
  617.             $this->addTextPart($alt);
  618.             $this->addHtmlPart($alt);
  619.  
  620.             // Attachments
  621.             $this->addAttachmentParts($message);
  622.             break;
  623.  
  624.          case $html AND $attachments AND $html_images :
  625.             $this->addMixedPart($message);
  626.             $rel $this->addRelatedPart($message);
  627.             $alt $this->addAlternativePart($rel);
  628.  
  629.             $this->addTextPart($alt);
  630.             $this->addHtmlPart($alt);
  631.  
  632.             // HTML images
  633.             $this->addHtmlImageParts($rel);
  634.  
  635.             // Attachments
  636.             $this->addAttachmentParts($message);
  637.             break;
  638.  
  639.       }
  640.  
  641.       if (isset ($message)) {
  642.          $output $message->encode();
  643.          $this->output $output['body'];
  644.          $this->headers array_merge($this->headers$output['headers']);
  645.  
  646.          // Figure out hostname
  647.          if (!empty ($_SERVER['HTTP_HOST'])) {
  648.             $hostname $_SERVER['HTTP_HOST'];
  649.  
  650.          else
  651.             if (!empty ($_SERVER['SERVER_NAME'])) {
  652.                $hostname $_SERVER['SERVER_NAME'];
  653.  
  654.             else
  655.                if (!empty ($_ENV['HOSTNAME'])) {
  656.                   $hostname $_ENV['HOSTNAME'];
  657.  
  658.                else {
  659.                   $hostname 'localhost';
  660.                }
  661.  
  662.          $message_id sprintf('<%s.%s@%s>'base_convert(time()1036)base_convert(rand()1036)$hostname);
  663.          $this->headers['Message-ID'$message_id;
  664.  
  665.          $this->is_built true;
  666.          return true;
  667.       else {
  668.          return false;
  669.       }
  670.    }
  671.  
  672.    /**
  673.    * Function to encode a header if necessary
  674.    * according to RFC2047
  675.    *
  676.    * @param  string $input   Value to encode
  677.    * @param  string $charset Character set to use
  678.    * @return string          Encoded value
  679.    */
  680.    private function encodeHeader($input$charset 'ISO-8859-1'{
  681.       preg_match_all('/(\w*[\x80-\xFF]+\w*)/'$input$matches);
  682.       foreach ($matches[1as $value{
  683.          $replacement preg_replace('/([\x80-\xFF])/e''"=" . strtoupper(dechex(ord("\1")))'$value);
  684.          $input str_replace($value'=?' $charset '?Q?' $replacement '?='$input);
  685.       }
  686.  
  687.       return $input;
  688.    }
  689.  
  690.    /**
  691.    * Sends the mail.
  692.    *
  693.    * @param  array  $recipients Array of receipients to send the mail to
  694.    * @param  string $type       How to send the mail ('mail' or 'sendmail' or 'smtp')
  695.    * @return mixed 
  696.    */
  697.    public function send($recipients$type 'mail'{
  698.       if (!defined('CRLF')) {
  699.          $this->setCRLF(($type == 'mail' OR $type == 'sendmail'"\n" "\r\n");
  700.       }
  701.  
  702.       if (!$this->is_built{
  703.          $this->build();
  704.       }
  705.  
  706.       switch ($type{
  707.          case 'mail' :
  708.             $subject '';
  709.             if (!empty ($this->headers['Subject'])) {
  710.                $subject $this->encodeHeader($this->headers['Subject']$this->build_params['head_charset']);
  711.                unset ($this->headers['Subject']);
  712.             }
  713.  
  714.             // Get flat representation of headers
  715.             foreach ($this->headers as $name => $value{
  716.                $headers[$name ': ' $this->encodeHeader($value$this->build_params['head_charset']);
  717.             }
  718.  
  719.             $to $this->encodeHeader(implode(', '$recipients)$this->build_params['head_charset']);
  720.  
  721.             if (!empty ($this->return_path)) {
  722.                $result mail($to$subject$this->outputimplode(CRLF$headers)'-f' $this->return_path);
  723.             else {
  724.                $result mail($to$subject$this->outputimplode(CRLF$headers));
  725.             }
  726.  
  727.             // Reset the subject in case mail is resent
  728.             if ($subject !== ''{
  729.                $this->headers['Subject'$subject;
  730.             }
  731.             // Return
  732.             return $result;
  733.  
  734.          case 'sendmail' :
  735.             // Get flat representation of headers
  736.             foreach ($this->headers as $name => $value{
  737.                $headers[$name ': ' $this->encodeHeader($value$this->build_params['head_charset']);
  738.             }
  739.  
  740.             // Encode To:
  741.             $headers['To: ' $this->encodeHeader(implode(', '$recipients)$this->build_params['head_charset']);
  742.  
  743.             // Get return path arg for sendmail command if necessary
  744.             $returnPath '';
  745.             if (!empty ($this->return_path)) {
  746.                $returnPath '-f' $this->return_path;
  747.             }
  748.  
  749.             $pipe popen($this->sendmail_path " " $returnPath'w');
  750.             $bytes fputs($pipeimplode(CRLF$headersCRLF CRLF $this->output);
  751.             $r pclose($pipe);
  752.             return $r;
  753.  
  754.          case 'smtp' :
  755.             $smtp src_mail_smtp :: connect($this->smtp_params);
  756.  
  757.             // Parse recipients argument for internet addresses
  758.             foreach ($recipients as $recipient{
  759.                $addresses src_mail_RFC822 :: parseAddressList($recipient$this->smtp_params['helo']nullfalse);
  760.                foreach ($addresses as $address{
  761.                   $smtp_recipients[sprintf('%s@%s'$address->mailbox$address->host);
  762.                }
  763.             }
  764.             unset ($addresses)// These are reused
  765.             unset ($address)// These are reused
  766.  
  767.             // Get flat representation of headers, parsing
  768.             // Cc and Bcc as we go
  769.             foreach ($this->headers as $name => $value{
  770.                if ($name == 'Cc' OR $name == 'Bcc'{
  771.                   $addresses src_mail_RFC822 :: parseAddressList($value$this->smtp_params['helo']nullfalse);
  772.                   foreach ($addresses as $address{
  773.                      $smtp_recipients[sprintf('%s@%s'$address->mailbox$address->host);
  774.                   }
  775.                }
  776.                if ($name == 'Bcc'{
  777.                   continue;
  778.                }
  779.                $headers[$name ': ' $this->encodeHeader($value$this->build_params['head_charset']);
  780.             }
  781.             // Add To header based on $recipients argument
  782.             $headers['To: ' $this->encodeHeader(implode(', '$recipients)$this->build_params['head_charset']);
  783.  
  784.             // Add headers to send_params
  785.             $send_params['headers'$headers;
  786.             $send_params['recipients'array_values(array_unique($smtp_recipients));
  787.             $send_params['body'$this->output;
  788.  
  789.             // Setup return path
  790.             if (isset ($this->return_path)) {
  791.                $send_params['from'$this->return_path;
  792.             }
  793.             elseif (!empty ($this->headers['From'])) {
  794.                $from src_mail_RFC822 :: parseAddressList($this->headers['From']);
  795.                $send_params['from'sprintf('%s@%s'$from[0]->mailbox$from[0]->host);
  796.             else {
  797.                $send_params['from''postmaster@' $this->smtp_params['helo'];
  798.             }
  799.  
  800.             // Send it
  801.             if (!$smtp->send($send_params)) {
  802.                $this->errors $smtp->getErrors();
  803.                return false;
  804.             }
  805.             return true;
  806.       }
  807.    }
  808.  
  809.    /**
  810.    * Use this method to return the email
  811.    * in message/rfc822 format. Useful for
  812.    * adding an email to another email as
  813.    * an attachment. there's a commented
  814.    * out example in example.php.
  815.    *
  816.    * @param array  $recipients Array of recipients
  817.    * @param string $type       Method to be used to send the mail.
  818.    *                            Used to determine the line ending type.
  819.    */
  820.    public function getRFC822($recipients$type 'mail'{
  821.       // Make up the date header as according to RFC822
  822.       $this->setHeader('Date'date('D, d M y H:i:s O'));
  823.  
  824.       if (!defined('CRLF')) {
  825.          $this->setCRLF($type == 'mail' "\n" "\r\n");
  826.       }
  827.  
  828.       if (!$this->is_built{
  829.          $this->build();
  830.       }
  831.  
  832.       // Return path ?
  833.       if (isset ($this->return_path)) {
  834.          $headers['Return-Path: ' $this->return_path;
  835.       }
  836.  
  837.       // Get flat representation of headers
  838.       foreach ($this->headers as $name => $value{
  839.          $headers[$name ': ' $value;
  840.       }
  841.       $headers['To: ' implode(', '$recipients);
  842.  
  843.       return implode(CRLF$headersCRLF CRLF $this->output;
  844.    }
  845. // End of class.
  846.  
  847. /**
  848. * Attachment classes
  849.  * @package src
  850.  * @subpackage mail
  851.  */
  852. class attachment {
  853.    /**
  854.    * Data of attachment
  855.    * @var string 
  856.    */
  857.    public $data;
  858.  
  859.    /**
  860.    * Name of attachment (filename)
  861.    * @var string 
  862.    */
  863.    public $name;
  864.  
  865.    /**
  866.    * Content type of attachment
  867.    * @var string 
  868.    */
  869.    public $contentType;
  870.  
  871.    /**
  872.    * Encoding type of attachment
  873.    * @var object 
  874.    */
  875.    public $encoding;
  876.  
  877.    /**
  878.    * Constructor
  879.    *
  880.    * @param string $data        File data
  881.    * @param string $name        Name of attachment (filename)
  882.    * @param string $contentType Content type of attachment
  883.    * @param object $encoding    Encoding type to use
  884.    */
  885.    public function __construct($data$name$contentTypeiEncoding $encoding{
  886.       $this->data = $data;
  887.       $this->name = $name;
  888.       $this->contentType = $contentType;
  889.       $this->encoding = $encoding;
  890.    }
  891. }
  892.  
  893. /**
  894. * File based attachment class
  895.  * @package src
  896.  * @subpackage mail
  897.  */
  898. class fileAttachment extends attachment {
  899.    /**
  900.    * Constructor
  901.    *
  902.    * @param string $filename    Name of file
  903.    * @param string $contentType Content type of file
  904.    * @param string $encoding    What encoding to use
  905.    */
  906.    public function __construct($filename$contentType 'application/octet-stream'$encoding null{
  907.       $encoding is_null($encodingnew Base64Encoding($encoding;
  908.  
  909.       parent :: __construct(file_get_contents($filename)basename($filename)$contentType$encoding);
  910.    }
  911. }
  912.  
  913. /**
  914. * Attachment class to handle attachments which are contained
  915. * in a variable.
  916.  * @package src
  917.  * @subpackage mail
  918.  */
  919. class stringAttachment extends attachment {
  920.    /**
  921.    * Constructor
  922.    *
  923.    * @param string $data        File data
  924.    * @param string $name        Name of attachment (filename)
  925.    * @param string $contentType Content type of file
  926.    * @param string $encoding    What encoding to use
  927.    */
  928.    public function __construct($data$name ''$contentType 'application/octet-stream'$encoding null{
  929.       $encoding is_null($encodingnew Base64Encoding($encoding;
  930.  
  931.       parent :: __construct($data$name$contentType$encoding);
  932.    }
  933. }
  934.  
  935. /**
  936. * File based embedded image class
  937.  * @package src
  938.  * @subpackage mail
  939.  */
  940. class fileEmbeddedImage extends fileAttachment {
  941. }
  942.  
  943. /**
  944. * String based embedded image class
  945.  * @package src
  946.  * @subpackage mail
  947.  */
  948. }
  949.  
  950. /**
  951. *
  952. */
  953. /**
  954. * Encoding interface
  955.  * @package src
  956.  * @subpackage mail
  957.  */
  958. interface iEncoding {
  959.    public function encode($input);
  960.    public function getType();
  961. }
  962.  
  963. /**
  964. * Base64 Encoding class
  965.  * @package src
  966.  * @subpackage mail
  967.  */
  968. class Base64Encoding implements iEncoding {
  969.    /*
  970.    * Function to encode data using
  971.    * base64 encoding.
  972.    *
  973.    * @param string $input Data to encode
  974.    */
  975.    public function encode($input{
  976.       return rtrim(chunk_split(base64_encode($input)76defined('MAIL_MIME_PART_CRLF'MAIL_MIME_PART_CRLF "\r\n"));
  977.    }
  978.  
  979.    /**
  980.    * Returns type
  981.    */
  982.    public function getType({
  983.       return 'base64';
  984.    }
  985. }
  986.  
  987. /**
  988. * Quoted Printable Encoding class
  989.  * @package src
  990.  * @subpackage mail
  991.  */
  992. class QPrintEncoding implements iEncoding {
  993.    /*
  994.    * Function to encode data using
  995.    * quoted-printable encoding.
  996.    *
  997.    * @param string $input Data to encode
  998.    */
  999.    public function encode($input{
  1000.       // Replace non printables
  1001.       $input preg_replace('/([^\x20\x21-\x3C\x3E-\x7E\x0A\x0D])/e''sprintf("=%02X", ord("\1"))'$input);
  1002.       $inputLen strlen($input);
  1003.       $outLines array ();
  1004.       $output '';
  1005.  
  1006.       $lines preg_split('/\r?\n/'$input);
  1007.  
  1008.       // Walk through each line
  1009.       for ($i 0$i count($lines)$i++{
  1010.          // Is line too long ?
  1011.          if (strlen($lines[$i]$lineMax{
  1012.             $outLines[substr($lines[$i]0$lineMax -1"="// \r\n Gets added when lines are imploded
  1013.             $lines[$isubstr($lines[$i]$lineMax -1);
  1014.             $i--// Ensure this line gets redone as we just changed it
  1015.          else {
  1016.             $outLines[$lines[$i];
  1017.          }
  1018.       }
  1019.  
  1020.       // Convert trailing whitespace
  1021.       $output preg_replace('/(\x20+)$/me''str_replace(" ", "=20", "\1")'$outLines);
  1022.  
  1023.       return implode("\r\n"$output);
  1024.    }
  1025.  
  1026.    /**
  1027.    * Returns type
  1028.    */
  1029.    public function getType({
  1030.       return 'quoted-printable';
  1031.    }
  1032. }
  1033.  
  1034. /**
  1035. * 7Bit Encoding class
  1036.  * @package src
  1037.  * @subpackage mail
  1038.  */
  1039. class SevenBitEncoding implements iEncoding {
  1040.    /*
  1041.    * Function to "encode" data using
  1042.    * 7bit encoding.
  1043.    *
  1044.    * @param string $input Data to encode
  1045.    */
  1046.    public function encode($input{
  1047.       return $input;
  1048.    }
  1049.  
  1050.    /**
  1051.    * Returns type
  1052.    */
  1053.    public function getType({
  1054.       return '7bit';
  1055.    }
  1056. }
  1057.  
  1058. /**
  1059. * 8Bit Encoding class
  1060.  * @package src
  1061.  * @subpackage mail
  1062.  */
  1063. class EightBitEncoding implements iEncoding {
  1064.    /*
  1065.    * Function to "encode" data using
  1066.    * 8bit encoding.
  1067.    *
  1068.    * @param string $input Data to encode
  1069.    */
  1070.    public function encode($input{
  1071.       return $input;
  1072.    }
  1073.  
  1074.    /**
  1075.    * Returns type
  1076.    */
  1077.    public function getType({
  1078.       return '8bit';
  1079.    }
  1080. }
  1081. ?>

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