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 /* $Id: MimeMail.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00014 
00018     final class MimeMail implements MailBuilder
00019     {
00020         private $parts = array();
00021 
00022         // should be built by build()
00023         private $body       = null;
00024         private $headers    = null;
00025         
00029         public function addPart(MimePart $part)
00030         {
00031             $this->parts[] = $part;
00032             
00033             return $this;
00034         }
00035         
00036         public function build()
00037         {
00038             if (!$this->parts)
00039                 throw new UnimplementedFeatureException();
00040             
00041             $boundary = '=_'.md5(microtime(true));
00042             
00043             $mail =
00044                 MimePart::create()->
00045                 setContentType('multipart/mixed')->
00046                 setBoundary($boundary);
00047             
00048             $this->headers =
00049                 "MIME-Version: 1.0\n"
00050                 .$mail->getHeaders();
00051             
00052             foreach ($this->parts as $part)
00053                 $this->body .=
00054                     '--'.$boundary."\n"
00055                     .$part->getHeaders()
00056                     ."\n\n"
00057                     .$part->getEncodedBody()."\n";
00058             
00059             $this->body .= '--'.$boundary."--"."\n\n";
00060         }
00061         
00062         public function getEncodedBody()
00063         {
00064             Assert::isTrue(
00065                 $this->body && $this->headers
00066             );
00067             
00068             return $this->body;
00069         }
00070         
00071         public function getHeaders()
00072         {
00073             Assert::isTrue(
00074                 $this->body && $this->headers
00075             );
00076             
00077             return $this->headers;
00078         }
00079     }
00080 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4