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