home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 March / Chip_2002-03_cd1.bin / internet / html / class.html.mime.mail.inc / __headers__.txt
Encoding:
Text File  |  2001-12-11  |  14.9 KB  |  165 lines

  1. <?php
  2. /***************************************
  3. ** Title.........: HTML Mime Mail class
  4. ** Version.......: 1.34
  5. ** Author........: Smejky (smejkyho@mal.cz)
  6. ** Filename......: html_mime_mail.class
  7. ** Last changed..: 28/01/01
  8. ** Notes.........: Based upon mime_mail.class
  9. ***************************************/
  10.  
  11. class html_mime_mail{
  12.  
  13.         var $mime;        var $html;        var $body;        var $do_html;        var $multipart;        var $html_text;        var $html_images;        var $headers;        var $parts;        var $charset;        var $charsetlist;  
  14. /***************************************
  15. ** Constructor function. Sets the headers
  16. ** if supplied.
  17. ***************************************/
  18.  
  19.         function html_mime_mail($headers = ''){
  20.  
  21.                 $this->html_images = array();                $this->headers     = array();                $this->parts       = array();                $this->charsetlist = array('iso'  => 'us-ascii',                                           'big5' => 'big5',                                           'gb'   => 'gb2312');  
  22.                 $this->charset     = 'us-ascii';  
  23.                 if($headers == '') return TRUE;                if(is_string($headers)) $headers = explode("\n", trim($headers));                for($i=0; $i<count($headers); $i++){                        if(is_array($headers[$i])) for($j=0; $j<count($headers[$i]); $j++) if($headers[$i][$j] != '') $this->headers[] = $headers[$i][$j];                        if($headers[$i] != '') $this->headers[] = $headers[$i];                }        }
  24.  
  25. /***************************************
  26. ** Accessor function to set the body text.
  27. ** Body text is used if it's not an html
  28. ** mail being sent.
  29. ***************************************/
  30.  
  31.         function set_body($text = ''){                if(is_string($text)){                        $this->body = $text;                        return TRUE;                }                return FALSE;        }
  32.  
  33. /***************************************
  34. ** Accessor function to return the mime
  35. ** class variable. Purely for debug.
  36. ***************************************/
  37.  
  38.         function get_mime(){                if(!isset($this->mime)) $this->mime = '';                return $this->mime;        }
  39.  
  40. /***************************************
  41. ** Function to set a header. Shouldn't
  42. ** really be necessary as you could use
  43. ** the constructor and send functions,
  44. ** it's here nonetheless. Takes any number
  45. ** of arguments, which can be either
  46. ** strings or arrays full of strings.
  47. ** this function is php4 only and will
  48. ** return false otherwise. Will return
  49. ** true upon finishing.
  50. ***************************************/
  51.  
  52.         function add_header(){                if((int)phpversion() < 4) return FALSE;                $args = func_get_args();                for($i=0; $i<count($args); $i++){                        if(is_array($args[$i])) for($j=0; $j<count($args[$i]); $j++) if($args[$i][$j] != '') $this->headers[] = $args[$i][$j];                        if($args[$i] != '') $this->headers[] = $args[$i];                }                return TRUE;        }
  53.  
  54. /***************************************
  55. ** Accessor function to set the content charset.
  56. ** Matt add 2000/10/19
  57. ***************************************/        function set_charset($charset = '', $raw = FALSE){
  58.  
  59.                 if($raw == TRUE){                        $this->charset = $charset;                        return TRUE;                }
  60.  
  61.                 if(is_string($charset)){                        while(list($k,$v) = each($this->charsetlist)){                                if($k == $charset){                                        $this->charset = $v;                                        return TRUE;                                }                        }            }            return FALSE;        }
  62.  
  63. /***************************************
  64. ** Adds a html part to the mail.
  65. ** Also replaces image names with
  66. ** content-id's.
  67. ***************************************/        function add_html($html, $text){                $this->do_html   = 1;                $this->html      = $html;                $this->html_text = $text;                if(is_array($this->html_images) AND count($this->html_images) > 0){                        for($i=0; $i<count($this->html_images); $i++) $this->html = ereg_replace($this->html_images[$i]['name'], 'cid:'.$this->html_images[$i]['cid'], $this->html);                }        }
  68.  
  69. /***************************************
  70. ** Builds html part of email.
  71. ***************************************/        function build_html($orig_boundary){                $sec_boundary = '=_'.md5(uniqid(time()));                $thr_boundary = '=_'.md5(uniqid(time()));  
  72.                 if(count($this->html_images) == 0){                        $this->multipart.= '--'.$orig_boundary."\n";                        $this->multipart.= 'Content-Type: multipart/alternative;'.chr(10).chr(9).'boundary="'.$sec_boundary."\"\n\n\n";  
  73.                         $this->multipart.= '--'.$sec_boundary."\n";                        $this->multipart.= 'Content-Type: text/plain; charset="'.$this->charset.'"'."\n";                        $this->multipart.= 'Content-Transfer-Encoding: base64'."\n\n";                        $this->multipart.= chunk_split(base64_encode($this->html_text))."\n\n";  
  74.                         $this->multipart.= '--'.$sec_boundary."\n";                        $this->multipart.= 'Content-Type: text/html; charset="'.$this->charset.'"'."\n";                        $this->multipart.= 'Content-Transfer-Encoding: base64'."\n\n";                        $this->multipart.= chunk_split(base64_encode($this->html))."\n\n";                        $this->multipart.= '--'.$sec_boundary."--\n\n";                }else{                        $this->multipart.= '--'.$orig_boundary."\n";                        $this->multipart.= 'Content-Type: multipart/related;'.chr(10).chr(9).'boundary="'.$sec_boundary."\"\n\n\n";  
  75.                         $this->multipart.= '--'.$sec_boundary."\n";                        $this->multipart.= 'Content-Type: multipart/alternative;'.chr(10).chr(9).'boundary="'.$thr_boundary."\"\n\n\n";  
  76.                         $this->multipart.= '--'.$thr_boundary."\n";                        $this->multipart.= 'Content-Type: text/plain; charset="'.$this->charset.'"'."\n";                        $this->multipart.= 'Content-Transfer-Encoding: base64'."\n\n";                        $this->multipart.= chunk_split(base64_encode($this->html_text))."\n\n";  
  77.                         $this->multipart.= '--'.$thr_boundary."\n";                        $this->multipart.= 'Content-Type: text/html'."\n";                        $this->multipart.= 'Content-Transfer-Encoding: base64'."\n\n";                        $this->multipart.= chunk_split(base64_encode($this->html))."\n\n";                        $this->multipart.= '--'.$thr_boundary."--\n\n";  
  78.                         for($i=0; $i<count($this->html_images); $i++){                                $this->multipart.= '--'.$sec_boundary."\n";                                $this->build_html_image($i);                        }
  79.  
  80.                         $this->multipart.= "--".$sec_boundary."--\n\n";                }        }
  81. /***************************************
  82. ** Adds an image to the list of embedded
  83. ** images.
  84. ***************************************/        function add_html_image($file, $name = '', $c_type='application/octet-stream'){                $this->html_images[] = array( 'body'   => $file,                                              'name'   => $name,                                              'c_type' => $c_type,                                              'cid'    => md5(uniqid(time())) );        }
  85.  
  86.  
  87. /***************************************
  88. ** Adds a file to the list of attachments.
  89. ***************************************/        function add_attachment($file, $name = '', $c_type='application/octet-stream'){                $this->parts[] = array( 'body'   => $file,                                        'name'   => $name,                                        'c_type' => $c_type );        }
  90.  
  91. /***************************************
  92. ** Builds an embedded image part of an
  93. ** html mail.
  94. ***************************************/        function build_html_image($i){                $this->multipart.= 'Content-Type: '.$this->html_images[$i]['c_type'];  
  95.                 if($this->html_images[$i]['name'] != '') $this->multipart .= '; name="'.$this->html_images[$i]['name']."\"\n";                else $this->multipart .= "\n";  
  96.                 $this->multipart.= 'Content-Transfer-Encoding: base64'."\n";                $this->multipart.= 'Content-ID: <'.$this->html_images[$i]['cid'].">\n\n";                $this->multipart.= chunk_split(base64_encode($this->html_images[$i]['body']))."\n";        }
  97.  
  98. /***************************************
  99. ** Builds a single part of a multipart
  100. ** message.
  101. ***************************************/        function build_part($i){                $message_part = '';                $message_part.= 'Content-Type: '.$this->parts[$i]['c_type'];                if($this->parts[$i]['name'] != '')                        $message_part .= '; name="'.$this->parts[$i]['name']."\"\n";                else                        $message_part .= "\n";  
  102.                 // Determine content encoding.                if($this->parts[$i]['c_type'] == 'text/plain'){                        $message_part.= 'Content-Transfer-Encoding: base64'."\n\n";                        $message_part.= chunk_split(base64_encode($this->parts[$i]['body']))."\n";                }elseif($this->parts[$i]['c_type'] == 'message/rfc822'){                        $message_part.= 'Content-Transfer-Encoding: 7bit'."\n\n";                        $message_part.= $this->parts[$i]['body']."\n";                }else{                        $message_part.= 'Content-Transfer-Encoding: base64'."\n";                        $message_part.= 'Content-Disposition: attachment; filename="'.$this->parts[$i]['name']."\"\n\n";                        $message_part.= chunk_split(base64_encode($this->parts[$i]['body']))."\n";                }
  103.  
  104.                 return $message_part;        }
  105.  
  106. /***************************************
  107. ** Builds the multipart message from the
  108. ** list ($this->_parts).
  109. ***************************************/        function build_message(){                $boundary = '=_'.md5(uniqid(time()));  
  110.                 $this->headers[] = 'MIME-Version: 1.0';                $this->headers[] = 'Content-Type: multipart/mixed;'.chr(10).chr(9).'boundary="'.$boundary.'"';                $this->multipart = "This is a MIME encoded message.\n\n";  
  111.                 if(isset($this->do_html) AND $this->do_html == 1) $this->build_html($boundary);                if(isset($this->body) AND $this->body != '') $this->parts[] = array('body' => $this->body, 'name' => '', 'c_type' => 'text/plain');  
  112.                 for($i=(count($this->parts)-1); $i>=0; $i--){                        $this->multipart.= '--'.$boundary."\n".$this->build_part($i);                }
  113.  
  114.                 $this->mime = $this->multipart."--".$boundary."--\n";        }
  115.  
  116. /***************************************
  117. ** Sends the mail.
  118. ***************************************/        function send($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = ''){
  119.  
  120.                 if($to_name != '') $to = '"'.$to_name.'" <'.$to_addr.'>';                else $to = $to_addr;  
  121.                 if($from_name != '') $from = '"'.$from_name.'" <'.$from_addr.'>';                else $from = $from_addr;  
  122.                 if(is_string($headers)) $headers = explode("\n", trim($headers));                for($i=0; $i<count($headers); $i++){                        if(is_array($headers[$i])) for($j=0; $j<count($headers[$i]); $j++) if($headers[$i][$j] != '') $xtra_headers[] = $headers[$i][$j];                        if($headers[$i] != '') $xtra_headers[] = $headers[$i];                }                if(!isset($xtra_headers)) $xtra_headers = array();  
  123.                 mail($to, $subject, $this->mime, 'From: '.$from."\n".implode("\n", $this->headers)."\n".implode("\n", $xtra_headers));        }
  124.  
  125. /***************************************
  126. ** Use this method to deliver using direct
  127. ** smtp connection. Relies upon Manuel Lemos'
  128. ** smtp mail delivery class available at:
  129. ** http://phpclasses.upperdesign.com
  130. **
  131. ** void smtp_send( string *Name* of smtp object,
  132. **                 string From address,
  133. **                 array  To addresses,
  134. **                 string Subject,
  135. **                 array  Extra headers)
  136. ***************************************/        function smtp_send($smtp_obj, $from_addr, $to_addr, $subject, $xtra_headers = ''){                global $$smtp_obj;                $smtp_obj = $$smtp_obj;  
  137.                 $headers   = $this->headers;                $headers[] = 'From: '.$from_addr;                $headers[] = 'Subject: '.$subject;                if(is_array($xtra_headers)) for(reset($xtra_headers); list(,$header) = each($xtra_headers); ) $headers[] = $header;  
  138.                 // the following: sendmessage(string from address, array to addresses, array headers, string body)                $smtp_obj->sendmessage($from_addr, $to_addr, $headers, $this->mime);        }
  139.  
  140. /***************************************
  141. ** Use this method to return the email
  142. ** in message/rfc822 format. Useful for
  143. ** adding an email to another email as
  144. ** an attachment. there's a commented
  145. ** out example in example.php.
  146. **
  147. ** string get_rfc822(string To name,
  148. **                   string To email,
  149. **                   string From name,
  150. **                   string From email,
  151. **                   [string Subject,
  152. **                    string Extra headers])
  153. ***************************************/        function get_rfc822($to_name, $to_addr, $from_name, $from_addr, $subject = '', $headers = ''){
  154.  
  155.                 // Make up the date header as according to RFC822                $date = 'Date: '.date('D, d M y H:i:s');  
  156.                 if($to_name != '') $to = 'To: "'.$to_name.'" <'.$to_addr.'>';                else $to = $to_addr;  
  157.                 if($from_name != '') $from = 'From: "'.$from_name.'" <'.$from_addr.'>';                else $from = $from_addr;  
  158.                 if(is_string($subject)) $subject = 'Subject: '.$subject;  
  159.                 if(is_string($headers)) $headers = explode("\n", trim($headers));                for($i=0; $i<count($headers); $i++){                        if(is_array($headers[$i])) for($j=0; $j<count($headers[$i]); $j++) if($headers[$i][$j] != '') $xtra_headers[] = $headers[$i][$j];                        if($headers[$i] != '') $xtra_headers[] = $headers[$i];                }                if(!isset($xtra_headers)) $xtra_headers = array();  
  160.                 return $date."\n".$from."\n".$to."\n".$subject."\n".implode("\n", $this->headers)."\n".implode("\n", $xtra_headers)."\n\n".$this->mime;        }
  161.  
  162.  
  163. } // End of class.
  164. ?>
  165.