home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / sendmail.class.inc < prev    next >
Text File  |  2004-03-08  |  8KB  |  307 lines

  1. <?php
  2. /*
  3.  * $Header: /cvsroot/group-office/groupoffice/classes/sendmail.class.inc,v 1.7 2004/01/19 18:13:57 mschering Exp $
  4.  *
  5.  * Copyright 2001 Nicolas Chalanset <nicocha@free.fr>
  6.  * Copyright 2001 Olivier Cahagne <cahagn_o@epita.fr>
  7.  *
  8.  * See the enclosed file COPYING for license information (GPL).  If you
  9.  * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  10.  */
  11.  
  12. class mime_mail
  13. {
  14.     var $parts;
  15.     var $to;
  16.     var $cc;
  17.     var $bcc;
  18.     var $from;
  19.     var $headers;
  20.     var $subject;
  21.     var $body;
  22.     var $smtp_server;
  23.     var $smtp_port;
  24.     var $charset;
  25.     var $crlf;
  26.     var $priority;
  27.     var $sender_name;
  28.     var $mime;
  29.     var $body_ctype;
  30.  
  31.   /*
  32.   *     void mime_mail()
  33.   *     class constructor
  34.   */
  35.     function mime_mail()
  36.     {
  37.         $this->parts = Array();
  38.         $this->to =  Array();
  39.         $this->cc = Array();
  40.         $this->bcc = Array();
  41.         $this->from =  null;
  42.         $this->headers = null;
  43.         $this->subject =  null;
  44.         $this->body =  null;
  45.         $this->smtp_server =  '';
  46.         $this->smtp_port = '';
  47.         $this->charset = 'iso-8859-1';
  48.         $this->crlf = null;
  49.         $this->priority = '3 (Normal)';
  50.         $this->body_ctype = "text/plain";
  51.     }
  52.  
  53.   /*
  54.   *     void add_attachment(string message, [string name], [string ctype], [string encoding], [string charset])
  55.   *     Add an attachment to the mail object
  56.   */
  57.     function add_attachment($message, $name, $ctype, $encoding, $charset, $disposition='', $content_id = '')
  58.     {
  59.  
  60.          $this->parts[] = array(
  61.                         'ctype' => $ctype,
  62.                         'message' => $message,
  63.                         'encoding' => $encoding,
  64.                         'disposition' => $disposition,
  65.                         'charset' => $charset,
  66.                         'name' => $name,
  67.                          'Content-ID' => $content_id);
  68.     }
  69.  
  70. /*
  71.  *      void build_message(array part)
  72.  *      Build message parts of a multipart mail
  73.  */
  74.     function build_message($part)
  75.     {
  76.         $message = $part['message'];
  77.         $encoding = $part['encoding'];
  78.         $charset = $part['charset'];
  79.         switch($encoding)
  80.         {
  81.             case 'base64':
  82.                 $message = chunk_split(base64_encode($message));
  83.                 break;
  84.             case 'quoted-printable':
  85.                 $message = imap_8bit($message);
  86.                 break;
  87.             default:
  88.                 break;
  89.         }
  90.         $val = 'Content-Type: ' . $part['ctype'] . ';';
  91.         $val .= ($part['charset'] ? $this->crlf . "\tcharset=\"" . $part['charset'] . '"' : '');
  92.         $val .= ($part['name'] ? $this->crlf . "\tname=\"" . $part['name'] . '"' : '');
  93.         $val .= $this->crlf . 'Content-Transfer-Encoding: ' . $encoding;
  94.         if ($part['Content-ID'] != '')
  95.         {
  96.             if (strpos($part['Content-ID'],'>'))
  97.             {
  98.                 $part['Content-ID'] = substr($part['Content-ID'], 1,strlen($part['Content-ID'])-2);
  99.             }
  100.             $part['Content-ID'] = $part['Content-ID'];
  101.  
  102.             $val .= $this->crlf.'Content-ID: <'.$part['Content-ID'].'>';
  103.         }
  104.         if ($part['disposition'] == 'attachment')
  105.         {
  106.             $val .= ($part['name'] ? $this->crlf . 'Content-Disposition: '.$part['disposition'].';' . $this->crlf . "\tfilename=\"" . $part['name'] . '"' : '');
  107.         }
  108.  
  109.         $val .= $this->crlf . $this->crlf . $message . $this->crlf;
  110.         return($val);
  111.     }
  112.  
  113. /*
  114.  *      void build_multipart()
  115.  *      Build a multipart mail
  116.  */
  117.     function build_multipart()
  118.     {
  119.         $boundary = '--=_GroupOfficeEmail_'.md5(uniqid(time()));
  120.         $multipart = "Content-Type: multipart/mixed;".$this->crlf."\tboundary=\"$boundary\"".$this->crlf.$this->crlf.'This is a MIME encoded message.'.$this->crlf.$this->crlf.'--'.$boundary;
  121.  
  122.         for($i = sizeof($this->parts) - 1; $i >= 0; $i--)
  123.         {
  124.             $multipart .= $this->crlf.$this->build_message($this->parts[$i]).'--'.$boundary;
  125.         }
  126.         return ($multipart .= '--' . $this->crlf);
  127.     }
  128.  
  129. /*
  130.  *        void build_body()
  131.  *        build a non multipart mail
  132. */
  133.  
  134.     function build_body()
  135.     {
  136.         if (sizeof($this->parts) == 1)
  137.             $part = $this->build_message($this->parts[0]);
  138.         else
  139.             $part = '';
  140.         return ($part . $this->crlf);
  141.     }
  142.  
  143.     function build_mime($smtp_server=false)
  144.     {
  145.         $mime = '';
  146.         $this->mime = '';
  147.  
  148.         if (isset($this->to[0]))
  149.         {
  150.             $this->mime .= 'To: <'.$this->to[0].'>';
  151.             for ($i=1;$i<count($this->to);$i++)
  152.             {
  153.                 $this->mime .= ', <'.$this->to[$i].'>';
  154.             }
  155.             $this->mime .= $this->crlf;
  156.         }
  157.  
  158.         if (!empty($this->subject))
  159.         {
  160.             $this->mime .= 'Subject: ' . $this->subject . $this->crlf;
  161.         }
  162.  
  163.  
  164.         if (!empty($this->from))
  165.         {
  166.             $mime .= "From: \"".$this->sender_name."\" <".$this->from .">". $this->crlf;
  167.         }
  168.         if (isset($this->cc[0]))
  169.         {
  170.             $mime .= 'Cc: <'.$this->cc[0].'>';
  171.             for ($i=1;$i<count($this->cc);$i++)
  172.             {
  173.                 $mime .= ', <'.$this->cc[$i].'>';
  174.             }
  175.             $mime .= $this->crlf;
  176.  
  177.         }
  178.         if (isset($this->bcc[0]))
  179.         {
  180.             $mime .= 'Bcc: <'.$this->bcc[0].'>';
  181.             for ($i=1;$i<count($this->bcc);$i++)
  182.             {
  183.                 $mime .= ', <'.$this->bcc[$i].'>';
  184.             }
  185.             $mime .= $this->crlf;
  186.         }
  187.         if (ereg("[4-9]\.[0-9]\.[4-9].*", phpversion()))
  188.         {
  189.             $mime .= 'Date: ' . date("r") . $this->crlf;
  190.         }else
  191.         {
  192.             $mime .= 'Date: ' . date("D, j M Y H:i:s T") . $this->crlf;
  193.         }
  194.         if (!empty($this->from))
  195.         {
  196.             $mime .= 'Reply-To:  "'.$this->sender_name.'" <'.$this->from.'>'.$this->crlf;
  197.             $mime .= 'Errors-To:  "'.$this->sender_name.'" <'.$this->from.'>'.$this->crlf;
  198.         }
  199.  
  200.         $mime .= 'X-Priority: '.$this->priority.$this->crlf;
  201.  
  202.         if (!empty($this->headers))
  203.         {
  204.             $mime .= $this->headers.$this->crlf;
  205.         }
  206.  
  207.         if (sizeof($this->parts) >= 1)
  208.         {
  209.             if (strtolower($this->body_ctype) == 'text/html')
  210.             {
  211.                 //$encoding = 'base64';
  212.                 $encoding = 'quoted-printable';
  213.             }else
  214.             {
  215.                 $encoding = 'quoted-printable';
  216.             }
  217.  
  218.             $this->add_attachment($this->body,  '', $this->body_ctype, $encoding, $this->charset);
  219.             $mime .= 'MIME-Version: 1.0' . $this->crlf . $this->build_multipart();
  220.         }else
  221.         {
  222.             if (strtolower($this->body_ctype) == 'text/html')
  223.             {
  224.                 //$encoding = 'base64';
  225.                 $encoding = 'quoted-printable';
  226.             }else
  227.             {
  228.                 $encoding = '8bit';
  229.             }
  230.  
  231.             $this->add_attachment($this->body,  '', $this->body_ctype, $encoding, $this->charset);
  232.             $mime .= 'MIME-Version: 1.0' . $this->crlf . $this->build_body();
  233.         }
  234.  
  235.         $this->mime .= $mime;
  236.  
  237.         if ($smtp_server)
  238.         {
  239.             return $this->mime;
  240.         }else
  241.         {
  242.             return $mime;
  243.         }
  244.     }
  245.  
  246. /*
  247.  *      void send()
  248.  *      Send the mail (last class-function to be called)
  249.  */
  250.  
  251.     function send()
  252.     {
  253.         // Whether or not to use SMTP or sendmail
  254.         if ($this->smtp_server == '' || $this->smtp_port == '')
  255.         {
  256.             $mime = $this->build_mime(false);
  257.  
  258.             $rcpt_to = '';
  259.             if (isset($this->to[0]))
  260.             {
  261.                 $rcpt_to.= '<'.$this->to[0].'>';
  262.                 for ($i=1;$i<count($this->to);$i++)
  263.                 {
  264.                     $rcpt_to .= ', <'.$this->to[$i].'>';
  265.                 }
  266.             }
  267.             return (mail($rcpt_to, $this->subject,  '', $mime,"-f".$this->from));
  268.         }else
  269.         {
  270.             $mime = $this->build_mime(true);
  271.             if (($smtp = new smtp()) != 0)
  272.             {
  273.                 $smtp->smtp_server = $this->smtp_server;
  274.                 $smtp->port = $this->smtp_port;
  275.                 $smtp->from = $this->from;
  276.                 $smtp->to = $this->to;
  277.                 $smtp->cc = $this->cc;
  278.                 $smtp->bcc = $this->bcc;
  279.                 $smtp->subject = $this->subject;
  280.                 $smtp->data = $mime;
  281.                 return ($smtp->send());
  282.             }
  283.             else
  284.                 return (false);
  285.         }
  286.     }
  287.  
  288.     function strip_comment_array($array) {
  289.         for($i = 0; $i < count($array); $i++) {
  290.             $array[$i] = $this->strip_comment($array[$i]);
  291.         }
  292.         return $array;
  293.     }
  294.  
  295.     function strip_comment($address) {
  296.         $pos = strrpos($address, '<');
  297.         if ($pos === false) {
  298.             return '<'.$address.'>';
  299.         }
  300.         else {
  301.             return substr($address, $pos);
  302.         }
  303.     }
  304.  
  305. }  // end of class
  306. ?>
  307.