# MimePart.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 MimePart implements MailBuilder
    00018     {
    00019         private $contentId      = null;
    00020         private $contentType    = null;
    00021         private $boundary       = null;
    00022
    00023         private $encoding       = null;
    00024         private $charset        = null;
    00025
    00026         private $filename       = null;
    00027         private $description    = null;
    00028
    00029         private $body           = null;
    00030
    00031         private $inline         = false;
    00032
    00033         // sub-parts aka childrens
    00034         private $parts          = array();
    00035
    00039         public static function create()
    00040         {
    00041             return new self;
    00042         }
    00043
    00044         public function __construct()
    00045         {
    00046             // useful defaults
    00047
    00048             $this->encoding     = MailEncoding::seven();
    00049             $this->contentType  = 'text/plain';
    00050         }
    00051
    00055         public function setBoundary($boundary)
    00056         {
    00057             $this->boundary = $boundary;
    00058
    00059             return $this;
    00060         }
    00061
    00062         public function getBoundary()
    00063         {
    00064             return $this->boundary;
    00065         }
    00066
    00067         public function getContentId()
    00068         {
    00069             return $this->contentId;
    00070         }
    00071
    00075         public function setContentId($id)
    00076         {
    00077             $this->contentId = $id;
    00078
    00079             return $this;
    00080         }
    00081
    00082         public function getContentType()
    00083         {
    00084             return $this->contentType;
    00085         }
    00086
    00090         public function setContentType($type)
    00091         {
    00092             $this->contentType = $type;
    00093
    00094             return $this;
    00095         }
    00096
    00100         public function getEncoding()
    00101         {
    00102             return $this->encoding;
    00103         }
    00104
    00108         public function setEncoding(MailEncoding $encoding)
    00109         {
    00110             $this->encoding = $encoding;
    00111
    00112             return $this;
    00113         }
    00114
    00115         public function getCharset()
    00116         {
    00117             return $this->charset;
    00118         }
    00119
    00123         public function setCharset($charset)
    00124         {
    00125             $this->charset = $charset;
    00126
    00127             return $this;
    00128         }
    00129
    00130         public function getFilename()
    00131         {
    00132             return $this->filename;
    00133         }
    00134
    00138         public function setFilename($name)
    00139         {
    00140             $this->filename = $name;
    00141
    00142             return $this;
    00143         }
    00144
    00145         public function getDescription()
    00146         {
    00147             return $this->description;
    00148         }
    00149
    00153         public function setDescription($description)
    00154         {
    00155             $this->description = $description;
    00156
    00157             return $this;
    00158         }
    00159
    00164         public function loadBodyFromFile($path)
    00165         {
    00166             Assert::isTrue(is_readable($path));
    00167
    00168             $this->body = file_get_contents($path);
    00169
    00170             return $this;
    00171         }
    00172
    00176         public function setBody($body)
    00177         {
    00178             $this->body = $body;
    00179
    00180             return $this;
    00181         }
    00182
    00183         public function getBody()
    00184         {
    00185             return $this->body;
    00186         }
    00187
    00191         public function addSubPart(MimePart $part)
    00192         {
    00193             $this->parts[] = $part;
    00194
    00195             return $this;
    00196         }
    00197
    00201         public function setInline($inline = true)
    00202         {
    00203             $this->inline = $inline;
    00204
    00205             return $this;
    00206         }
    00207
    00208         public function getEncodedBody()
    00209         {
    00210             $body = null;
    00211
    00212             switch ($this->encoding->getId()) {
    00213                 case MailEncoding::SEVEN_BITS:
    00214                 case MailEncoding::EIGHT_BITS:
    00215
    00216                     $body = $this->body;
    00217                     break;
    00218
    00224                 case MailEncoding::QUOTED:
    00225
    00226                     $string =
    00227                         preg_replace(
    00228                             '/[^\x21-\x3C\x3E-\x7E\x09\x20]/e',
    00229                             'sprintf("=%02x", ord ("$0"));',
    00230                             $this->body
    00231                         );
    00232
    00233                     $matches = array();
    00234
    00235                     preg_match_all('/.{1,73}([^=]{0,3})?/', $string, $matches);
    00236
    00237                     $body = implode("=\n", $matches[0]);
    00238
    00239                     break;
    00240
    00241                 case MailEncoding::BASE64:
    00242
    00243                     $body =
    00244                         rtrim(
    00245                             chunk_split(
    00246                                 base64_encode($this->body),
    00247                                 76,
    00248                                 "\n"
    00249                             )
    00250                         );
    00251
    00252                     break;
    00253
    00254                 default:
    00255                     throw new WrongStateException('unknown mail encoding given');
    00256             }
    00257
    00258             return $body;
    00259         }
    00260
    00261         public function getHeaders()
    00262         {
    00263             $headers = array();
    00264
    00265             if ($this->contentType) {
    00266                 $header =
    00267                     "Content-Type: {$this->contentType};";
    00268
    00269                 if ($this->charset)
    00270                     $header .= " charset=\"{$this->charset}\"";
    00271
    00272                 if ($this->boundary)
    00273                     $header .= "\n\tboundary=\"{$this->boundary}\"";
    00274
    00275                 $headers[] = $header;
    00276             }
    00277
    00278             $headers[] = "Content-Transfer-Encoding: {$this->encoding->toString()}";
    00279
    00280             if ($this->contentId)
    00281                 $headers[] = "Content-ID: <{$this->contentId}>";
    00282
    00283             if (!$this->inline && $this->filename)
    00284                 $headers[] =
    00285                     "Content-Disposition: attachment; "
    00286                     ."filename=\"{$this->filename}\"";
    00287             elseif ($this->inline)
    00288                 $headers[] = 'Content-Disposition: inline';
    00289
    00290             if ($this->description)
    00291                 $headers[] = "Content-Description: {$this->description}";
    00292
    00293             return implode("\n", $headers);
    00294         }
    00295     }
    00296 ?>