MimeMail.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   Based on PEAR's Mail::MIME by Richard Heyes                           *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00017     final class MimeMail implements MailBuilder
00018     {
00019         private $parts = array();
00020 
00021         // should be built by build()
00022         private $body       = null;
00023         private $headers    = null;
00024         
00025         private $boundary   = null;
00026         
00030         public function addPart(MimePart $part)
00031         {
00032             $this->parts[] = $part;
00033             
00034             return $this;
00035         }
00036         
00037         public function build()
00038         {
00039             if (!$this->parts)
00040                 throw new UnimplementedFeatureException();
00041             
00042             if (!$this->boundary)
00043                 $this->boundary = '=_'.md5(microtime(true));
00044             
00045             $mail =
00046                 MimePart::create()->
00047                 setContentType('multipart/mixed')->
00048                 setBoundary($this->boundary);
00049             
00050             $this->headers =
00051                 "MIME-Version: 1.0\n"
00052                 .$mail->getHeaders();
00053 
00054             foreach ($this->parts as $part)
00055                 $this->body .=
00056                     '--'.$this->boundary."\n"
00057                     .$part->getHeaders()
00058                     ."\n\n"
00059                     .$part->getEncodedBody()."\n";
00060             
00061             $this->body .= '--'.$this->boundary."--"."\n\n";
00062         }
00063         
00064         public function getEncodedBody()
00065         {
00066             Assert::isTrue(
00067                 $this->body && $this->headers
00068             );
00069             
00070             return $this->body;
00071         }
00072         
00073         public function getHeaders()
00074         {
00075             Assert::isTrue(
00076                 $this->body && $this->headers
00077             );
00078             
00079             return $this->headers;
00080         }
00081         
00085         public function setBoundary($boundary)
00086         {
00087             $this->boundary = $boundary;
00088             
00089             return $this;
00090         }
00091         
00092         public function getBoundary()
00093         {
00094             return $this->boundary;
00095         }
00096     }
00097 ?>