Back to index

awl  0.53
Multipart.php
Go to the documentation of this file.
00001 <?php
00002 
00003 require_once('AWLUtilities.php');
00004 
00005 class SinglePart {
00006   private $content;
00007   private $type;
00008   private $otherHeaders;
00009   private $disposition;
00010   private $id;
00011 
00012   public static $crlf = "\r\n";
00013   
00014   function __construct( $content, $type='text/plain', $otherHeaders=array() ) {
00015     $this->content = $content;
00016     $this->type = $type;
00017     $this->otherHeaders = $otherHeaders;
00018   }
00019   
00020   function render() {
00021     $result = 'Content-Type: '.$this->type.self::$crlf;
00022     $encoded = false;
00023     foreach( $this->otherHeaders AS $header => $value ) {
00024       $result .= $header.': '.$value.self::$crlf;
00025       if ( $header == 'Content-Transfer-Encoding' ) $encoded = true;
00026     }
00027 
00028     if ( $encoded )
00029        return $result . self::$crlf . $content;
00030 
00031     return $result . 'Content-Transfer-Encoding: base64' . self::$crlf
00032                     . self::$crlf
00033                     . base64_encode($content);
00034   }
00035 }
00036 
00037 
00038 class Multipart {
00039 
00040   private $parts; // Always good for a giggle :-)
00041   private $boundary;
00042 
00043   function __construct() {
00044     $this->parts = array();
00045     $this->boundary = uuid();
00046   }
00047 
00048   function addPart() {
00049     $args = func_get_args();
00050     if ( is_string($args[0]) ) {
00051       $newPart = new SinglePart( $args[0], (isset($args[1])?$args[1]:'text/plain'), (isset($args[2])?$args[2]:array())); 
00052     }
00053     else
00054       $newPart = $args[0];
00055         
00056     $this->parts[] = $newPart;
00057     
00058     return $newPart;
00059   }
00060 
00061   
00062   function getMimeHeaders() {
00063     return 'MIME-Version: 1.0' . SinglePart::$crlf
00064           .'Content-Type: multipart/mixed; boundary='.$this->boundary . SinglePart::$crlf ;
00065   }
00066 
00067   function getMimeParts() {
00068     $result = '--' . $this->boundary . SinglePart::$crlf;
00069     foreach( $this->parts AS $part ) {
00070       $result .= $part->render() . SinglePart::$crlf . '--' . $this->boundary;
00071     }
00072     $result .= '--' . SinglePart::$crlf;
00073     return $result;
00074   }
00075 
00076 }