|
TYPO3 API
SVNRelease
|
00001 <?php 00002 00003 /* 00004 * This file is part of SwiftMailer. 00005 * (c) 2004-2009 Chris Corbyn 00006 * 00007 * For the full copyright and license information, please view the LICENSE 00008 * file that was distributed with this source code. 00009 */ 00010 00011 //@require 'Swift/Mime/Message.php'; 00012 //@require 'Swift/Mime/MimePart.php'; 00013 //@require 'Swift/Mime/MimeEntity.php'; 00014 //@require 'Swift/Mime/HeaderSet.php'; 00015 //@require 'Swift/Mime/ContentEncoder.php'; 00016 00017 /** 00018 * The default email message class. 00019 * @package Swift 00020 * @subpackage Mime 00021 * @author Chris Corbyn 00022 */ 00023 class Swift_Mime_SimpleMessage extends Swift_Mime_MimePart 00024 implements Swift_Mime_Message 00025 { 00026 00027 /** 00028 * Create a new SimpleMessage with $headers, $encoder and $cache. 00029 * @param Swift_Mime_HeaderSet $headers 00030 * @param Swift_Mime_ContentEncoder $encoder 00031 * @param Swift_KeyCache $cache 00032 * @param string $charset 00033 */ 00034 public function __construct(Swift_Mime_HeaderSet $headers, 00035 Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, $charset = null) 00036 { 00037 parent::__construct($headers, $encoder, $cache, $charset); 00038 $this->getHeaders()->defineOrdering(array( 00039 'Return-Path', 00040 'Sender', 00041 'Message-ID', 00042 'Date', 00043 'Subject', 00044 'From', 00045 'Reply-To', 00046 'To', 00047 'Cc', 00048 'Bcc', 00049 'MIME-Version', 00050 'Content-Type', 00051 'Content-Transfer-Encoding' 00052 )); 00053 $this->getHeaders()->setAlwaysDisplayed( 00054 array('Date', 'Message-ID', 'From') 00055 ); 00056 $this->getHeaders()->addTextHeader('MIME-Version', '1.0'); 00057 $this->setDate(time()); 00058 $this->setId($this->getId()); 00059 $this->getHeaders()->addMailboxHeader('From'); 00060 } 00061 00062 /** 00063 * Always returns {@link LEVEL_TOP} for a message instance. 00064 * @return int 00065 */ 00066 public function getNestingLevel() 00067 { 00068 return self::LEVEL_TOP; 00069 } 00070 00071 /** 00072 * Set the subject of this message. 00073 * @param string $subject 00074 */ 00075 public function setSubject($subject) 00076 { 00077 if (!$this->_setHeaderFieldModel('Subject', $subject)) 00078 { 00079 $this->getHeaders()->addTextHeader('Subject', $subject); 00080 } 00081 return $this; 00082 } 00083 00084 /** 00085 * Get the subject of this message. 00086 * @return string 00087 */ 00088 public function getSubject() 00089 { 00090 return $this->_getHeaderFieldModel('Subject'); 00091 } 00092 00093 /** 00094 * Set the date at which this message was created. 00095 * @param int $date 00096 */ 00097 public function setDate($date) 00098 { 00099 if (!$this->_setHeaderFieldModel('Date', $date)) 00100 { 00101 $this->getHeaders()->addDateHeader('Date', $date); 00102 } 00103 return $this; 00104 } 00105 00106 /** 00107 * Get the date at which this message was created. 00108 * @return int 00109 */ 00110 public function getDate() 00111 { 00112 return $this->_getHeaderFieldModel('Date'); 00113 } 00114 00115 /** 00116 * Set the return-path (the bounce address) of this message. 00117 * @param string $address 00118 */ 00119 public function setReturnPath($address) 00120 { 00121 if (!$this->_setHeaderFieldModel('Return-Path', $address)) 00122 { 00123 $this->getHeaders()->addPathHeader('Return-Path', $address); 00124 } 00125 return $this; 00126 } 00127 00128 /** 00129 * Get the return-path (bounce address) of this message. 00130 * @return string 00131 */ 00132 public function getReturnPath() 00133 { 00134 return $this->_getHeaderFieldModel('Return-Path'); 00135 } 00136 00137 /** 00138 * Set the sender of this message. 00139 * This does not override the From field, but it has a higher significance. 00140 * @param string $sender 00141 * @param string $name optional 00142 */ 00143 public function setSender($address, $name = null) 00144 { 00145 if (!is_array($address) && isset($name)) 00146 { 00147 $address = array($address => $name); 00148 } 00149 00150 if (!$this->_setHeaderFieldModel('Sender', (array) $address)) 00151 { 00152 $this->getHeaders()->addMailboxHeader('Sender', (array) $address); 00153 } 00154 return $this; 00155 } 00156 00157 /** 00158 * Get the sender of this message. 00159 * @return string 00160 */ 00161 public function getSender() 00162 { 00163 return $this->_getHeaderFieldModel('Sender'); 00164 } 00165 00166 /** 00167 * Add a From: address to this message. 00168 * 00169 * If $name is passed this name will be associated with the address. 00170 * 00171 * @param string $address 00172 * @param string $name optional 00173 */ 00174 public function addFrom($address, $name = null) 00175 { 00176 $current = $this->getFrom(); 00177 $current[$address] = $name; 00178 return $this->setFrom($current); 00179 } 00180 00181 /** 00182 * Set the from address of this message. 00183 * 00184 * You may pass an array of addresses if this message is from multiple people. 00185 * 00186 * If $name is passed and the first parameter is a string, this name will be 00187 * associated with the address. 00188 * 00189 * @param string $addresses 00190 * @param string $name optional 00191 */ 00192 public function setFrom($addresses, $name = null) 00193 { 00194 if (!is_array($addresses) && isset($name)) 00195 { 00196 $addresses = array($addresses => $name); 00197 } 00198 00199 if (!$this->_setHeaderFieldModel('From', (array) $addresses)) 00200 { 00201 $this->getHeaders()->addMailboxHeader('From', (array) $addresses); 00202 } 00203 return $this; 00204 } 00205 00206 /** 00207 * Get the from address of this message. 00208 * 00209 * @return string 00210 */ 00211 public function getFrom() 00212 { 00213 return $this->_getHeaderFieldModel('From'); 00214 } 00215 00216 /** 00217 * Add a Reply-To: address to this message. 00218 * 00219 * If $name is passed this name will be associated with the address. 00220 * 00221 * @param string $address 00222 * @param string $name optional 00223 */ 00224 public function addReplyTo($address, $name = null) 00225 { 00226 $current = $this->getReplyTo(); 00227 $current[$address] = $name; 00228 return $this->setReplyTo($current); 00229 } 00230 00231 /** 00232 * Set the reply-to address of this message. 00233 * 00234 * You may pass an array of addresses if replies will go to multiple people. 00235 * 00236 * If $name is passed and the first parameter is a string, this name will be 00237 * associated with the address. 00238 * 00239 * @param string $addresses 00240 * @param string $name optional 00241 */ 00242 public function setReplyTo($addresses, $name = null) 00243 { 00244 if (!is_array($addresses) && isset($name)) 00245 { 00246 $addresses = array($addresses => $name); 00247 } 00248 00249 if (!$this->_setHeaderFieldModel('Reply-To', (array) $addresses)) 00250 { 00251 $this->getHeaders()->addMailboxHeader('Reply-To', (array) $addresses); 00252 } 00253 return $this; 00254 } 00255 00256 /** 00257 * Get the reply-to address of this message. 00258 * 00259 * @return string 00260 */ 00261 public function getReplyTo() 00262 { 00263 return $this->_getHeaderFieldModel('Reply-To'); 00264 } 00265 00266 /** 00267 * Add a To: address to this message. 00268 * 00269 * If $name is passed this name will be associated with the address. 00270 * 00271 * @param string $address 00272 * @param string $name optional 00273 */ 00274 public function addTo($address, $name = null) 00275 { 00276 $current = $this->getTo(); 00277 $current[$address] = $name; 00278 return $this->setTo($current); 00279 } 00280 00281 /** 00282 * Set the to addresses of this message. 00283 * 00284 * If multiple recipients will receive the message and array should be used. 00285 * 00286 * If $name is passed and the first parameter is a string, this name will be 00287 * associated with the address. 00288 * 00289 * @param array $addresses 00290 * @param string $name optional 00291 */ 00292 public function setTo($addresses, $name = null) 00293 { 00294 if (!is_array($addresses) && isset($name)) 00295 { 00296 $addresses = array($addresses => $name); 00297 } 00298 00299 if (!$this->_setHeaderFieldModel('To', (array) $addresses)) 00300 { 00301 $this->getHeaders()->addMailboxHeader('To', (array) $addresses); 00302 } 00303 return $this; 00304 } 00305 00306 /** 00307 * Get the To addresses of this message. 00308 * 00309 * @return array 00310 */ 00311 public function getTo() 00312 { 00313 return $this->_getHeaderFieldModel('To'); 00314 } 00315 00316 /** 00317 * Add a Cc: address to this message. 00318 * 00319 * If $name is passed this name will be associated with the address. 00320 * 00321 * @param string $address 00322 * @param string $name optional 00323 */ 00324 public function addCc($address, $name = null) 00325 { 00326 $current = $this->getCc(); 00327 $current[$address] = $name; 00328 return $this->setCc($current); 00329 } 00330 00331 /** 00332 * Set the Cc addresses of this message. 00333 * 00334 * If $name is passed and the first parameter is a string, this name will be 00335 * associated with the address. 00336 * 00337 * @param array $addresses 00338 * @param string $name optional 00339 */ 00340 public function setCc($addresses, $name = null) 00341 { 00342 if (!is_array($addresses) && isset($name)) 00343 { 00344 $addresses = array($addresses => $name); 00345 } 00346 00347 if (!$this->_setHeaderFieldModel('Cc', (array) $addresses)) 00348 { 00349 $this->getHeaders()->addMailboxHeader('Cc', (array) $addresses); 00350 } 00351 return $this; 00352 } 00353 00354 /** 00355 * Get the Cc address of this message. 00356 * 00357 * @return array 00358 */ 00359 public function getCc() 00360 { 00361 return $this->_getHeaderFieldModel('Cc'); 00362 } 00363 00364 /** 00365 * Add a Bcc: address to this message. 00366 * 00367 * If $name is passed this name will be associated with the address. 00368 * 00369 * @param string $address 00370 * @param string $name optional 00371 */ 00372 public function addBcc($address, $name = null) 00373 { 00374 $current = $this->getBcc(); 00375 $current[$address] = $name; 00376 return $this->setBcc($current); 00377 } 00378 00379 /** 00380 * Set the Bcc addresses of this message. 00381 * 00382 * If $name is passed and the first parameter is a string, this name will be 00383 * associated with the address. 00384 * 00385 * @param array $addresses 00386 * @param string $name optional 00387 */ 00388 public function setBcc($addresses, $name = null) 00389 { 00390 if (!is_array($addresses) && isset($name)) 00391 { 00392 $addresses = array($addresses => $name); 00393 } 00394 00395 if (!$this->_setHeaderFieldModel('Bcc', (array) $addresses)) 00396 { 00397 $this->getHeaders()->addMailboxHeader('Bcc', (array) $addresses); 00398 } 00399 return $this; 00400 } 00401 00402 /** 00403 * Get the Bcc addresses of this message. 00404 * 00405 * @return array 00406 */ 00407 public function getBcc() 00408 { 00409 return $this->_getHeaderFieldModel('Bcc'); 00410 } 00411 00412 /** 00413 * Set the priority of this message. 00414 * The value is an integer where 1 is the highest priority and 5 is the lowest. 00415 * @param int $priority 00416 */ 00417 public function setPriority($priority) 00418 { 00419 $priorityMap = array( 00420 1 => 'Highest', 00421 2 => 'High', 00422 3 => 'Normal', 00423 4 => 'Low', 00424 5 => 'Lowest' 00425 ); 00426 $pMapKeys = array_keys($priorityMap); 00427 if ($priority > max($pMapKeys)) 00428 { 00429 $priority = max($pMapKeys); 00430 } 00431 elseif ($priority < min($pMapKeys)) 00432 { 00433 $priority = min($pMapKeys); 00434 } 00435 if (!$this->_setHeaderFieldModel('X-Priority', 00436 sprintf('%d (%s)', $priority, $priorityMap[$priority]))) 00437 { 00438 $this->getHeaders()->addTextHeader('X-Priority', 00439 sprintf('%d (%s)', $priority, $priorityMap[$priority])); 00440 } 00441 return $this; 00442 } 00443 00444 /** 00445 * Get the priority of this message. 00446 * The returned value is an integer where 1 is the highest priority and 5 00447 * is the lowest. 00448 * @return int 00449 */ 00450 public function getPriority() 00451 { 00452 list($priority) = sscanf($this->_getHeaderFieldModel('X-Priority'), 00453 '%[1-5]' 00454 ); 00455 return isset($priority) ? $priority : 3; 00456 } 00457 00458 /** 00459 * Ask for a delivery receipt from the recipient to be sent to $addresses 00460 * @param array $addresses 00461 */ 00462 public function setReadReceiptTo($addresses) 00463 { 00464 if (!$this->_setHeaderFieldModel('Disposition-Notification-To', $addresses)) 00465 { 00466 $this->getHeaders() 00467 ->addMailboxHeader('Disposition-Notification-To', $addresses); 00468 } 00469 return $this; 00470 } 00471 00472 /** 00473 * Get the addresses to which a read-receipt will be sent. 00474 * @return string 00475 */ 00476 public function getReadReceiptTo() 00477 { 00478 return $this->_getHeaderFieldModel('Disposition-Notification-To'); 00479 } 00480 00481 /** 00482 * Attach a {@link Swift_Mime_MimeEntity} such as an Attachment or MimePart. 00483 * @param Swift_Mime_MimeEntity $entity 00484 */ 00485 public function attach(Swift_Mime_MimeEntity $entity) 00486 { 00487 $this->setChildren(array_merge($this->getChildren(), array($entity))); 00488 return $this; 00489 } 00490 00491 /** 00492 * Remove an already attached entity. 00493 * @param Swift_Mime_MimeEntity $entity 00494 */ 00495 public function detach(Swift_Mime_MimeEntity $entity) 00496 { 00497 $newChildren = array(); 00498 foreach ($this->getChildren() as $child) 00499 { 00500 if ($entity !== $child) 00501 { 00502 $newChildren[] = $child; 00503 } 00504 } 00505 $this->setChildren($newChildren); 00506 return $this; 00507 } 00508 00509 /** 00510 * Attach a {@link Swift_Mime_MimeEntity} and return it's CID source. 00511 * This method should be used when embedding images or other data in a message. 00512 * @param Swift_Mime_MimeEntity $entity 00513 * @return string 00514 */ 00515 public function embed(Swift_Mime_MimeEntity $entity) 00516 { 00517 $this->attach($entity); 00518 return 'cid:' . $entity->getId(); 00519 } 00520 00521 /** 00522 * Get this message as a complete string. 00523 * @return string 00524 */ 00525 public function toString() 00526 { 00527 if (count($children = $this->getChildren()) > 0 && $this->getBody() != '') 00528 { 00529 $this->setChildren(array_merge(array($this->_becomeMimePart()), $children)); 00530 $string = parent::toString(); 00531 $this->setChildren($children); 00532 } 00533 else 00534 { 00535 $string = parent::toString(); 00536 } 00537 return $string; 00538 } 00539 00540 /** 00541 * Returns a string representation of this object. 00542 * 00543 * @return string 00544 * 00545 * @see toString() 00546 */ 00547 public function __toString() 00548 { 00549 return $this->toString(); 00550 } 00551 00552 /** 00553 * Write this message to a {@link Swift_InputByteStream}. 00554 * @param Swift_InputByteStream $is 00555 */ 00556 public function toByteStream(Swift_InputByteStream $is) 00557 { 00558 if (count($children = $this->getChildren()) > 0 && $this->getBody() != '') 00559 { 00560 $this->setChildren(array_merge(array($this->_becomeMimePart()), $children)); 00561 parent::toByteStream($is); 00562 $this->setChildren($children); 00563 } 00564 else 00565 { 00566 parent::toByteStream($is); 00567 } 00568 } 00569 00570 // -- Protected methods 00571 00572 /** @see Swift_Mime_SimpleMimeEntity::_getIdField() */ 00573 protected function _getIdField() 00574 { 00575 return 'Message-ID'; 00576 } 00577 00578 // -- Private methods 00579 00580 /** Turn the body of this message into a child of itself if needed */ 00581 private function _becomeMimePart() 00582 { 00583 $part = new parent($this->getHeaders()->newInstance(), $this->getEncoder(), 00584 $this->_getCache(), $this->_userCharset 00585 ); 00586 $part->setContentType($this->_userContentType); 00587 $part->setBody($this->getBody()); 00588 $part->setFormat($this->_userFormat); 00589 $part->setDelSp($this->_userDelSp); 00590 $part->_setNestingLevel($this->_getTopNestingLevel()); 00591 return $part; 00592 } 00593 00594 /** Get the highest nesting level nested inside this message */ 00595 private function _getTopNestingLevel() 00596 { 00597 $highestLevel = $this->getNestingLevel(); 00598 foreach ($this->getChildren() as $child) 00599 { 00600 $childLevel = $child->getNestingLevel(); 00601 if ($highestLevel < $childLevel) 00602 { 00603 $highestLevel = $childLevel; 00604 } 00605 } 00606 return $highestLevel; 00607 } 00608 00609 }
1.8.0