[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/Swiftmailer/classes/Swift/Mime/ -> SimpleMessage.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of SwiftMailer.
   5   * (c) 2004-2009 Chris Corbyn
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * The default email message class.
  13   *
  14   * @author Chris Corbyn
  15   */
  16  class Swift_Mime_SimpleMessage extends Swift_Mime_MimePart implements Swift_Mime_Message
  17  {
  18      /**
  19       * Create a new SimpleMessage with $headers, $encoder and $cache.
  20       *
  21       * @param Swift_Mime_HeaderSet      $headers
  22       * @param Swift_Mime_ContentEncoder $encoder
  23       * @param Swift_KeyCache            $cache
  24       * @param Swift_Mime_Grammar        $grammar
  25       * @param string                    $charset
  26       */
  27      public function __construct(Swift_Mime_HeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_Mime_Grammar $grammar, $charset = null)
  28      {
  29          parent::__construct($headers, $encoder, $cache, $grammar, $charset);
  30          $this->getHeaders()->defineOrdering(array(
  31              'Return-Path',
  32              'Received',
  33              'DKIM-Signature',
  34              'DomainKey-Signature',
  35              'Sender',
  36              'Message-ID',
  37              'Date',
  38              'Subject',
  39              'From',
  40              'Reply-To',
  41              'To',
  42              'Cc',
  43              'Bcc',
  44              'MIME-Version',
  45              'Content-Type',
  46              'Content-Transfer-Encoding',
  47              ));
  48          $this->getHeaders()->setAlwaysDisplayed(array('Date', 'Message-ID', 'From'));
  49          $this->getHeaders()->addTextHeader('MIME-Version', '1.0');
  50          $this->setDate(time());
  51          $this->setId($this->getId());
  52          $this->getHeaders()->addMailboxHeader('From');
  53      }
  54  
  55      /**
  56       * Always returns {@link LEVEL_TOP} for a message instance.
  57       *
  58       * @return int
  59       */
  60      public function getNestingLevel()
  61      {
  62          return self::LEVEL_TOP;
  63      }
  64  
  65      /**
  66       * Set the subject of this message.
  67       *
  68       * @param string $subject
  69       *
  70       * @return Swift_Mime_SimpleMessage
  71       */
  72      public function setSubject($subject)
  73      {
  74          if (!$this->_setHeaderFieldModel('Subject', $subject)) {
  75              $this->getHeaders()->addTextHeader('Subject', $subject);
  76          }
  77  
  78          return $this;
  79      }
  80  
  81      /**
  82       * Get the subject of this message.
  83       *
  84       * @return string
  85       */
  86      public function getSubject()
  87      {
  88          return $this->_getHeaderFieldModel('Subject');
  89      }
  90  
  91      /**
  92       * Set the date at which this message was created.
  93       *
  94       * @param int $date
  95       *
  96       * @return Swift_Mime_SimpleMessage
  97       */
  98      public function setDate($date)
  99      {
 100          if (!$this->_setHeaderFieldModel('Date', $date)) {
 101              $this->getHeaders()->addDateHeader('Date', $date);
 102          }
 103  
 104          return $this;
 105      }
 106  
 107      /**
 108       * Get the date at which this message was created.
 109       *
 110       * @return int
 111       */
 112      public function getDate()
 113      {
 114          return $this->_getHeaderFieldModel('Date');
 115      }
 116  
 117      /**
 118       * Set the return-path (the bounce address) of this message.
 119       *
 120       * @param string $address
 121       *
 122       * @return Swift_Mime_SimpleMessage
 123       */
 124      public function setReturnPath($address)
 125      {
 126          if (!$this->_setHeaderFieldModel('Return-Path', $address)) {
 127              $this->getHeaders()->addPathHeader('Return-Path', $address);
 128          }
 129  
 130          return $this;
 131      }
 132  
 133      /**
 134       * Get the return-path (bounce address) of this message.
 135       *
 136       * @return string
 137       */
 138      public function getReturnPath()
 139      {
 140          return $this->_getHeaderFieldModel('Return-Path');
 141      }
 142  
 143      /**
 144       * Set the sender of this message.
 145       *
 146       * This does not override the From field, but it has a higher significance.
 147       *
 148       * @param string $address
 149       * @param string $name    optional
 150       *
 151       * @return Swift_Mime_SimpleMessage
 152       */
 153      public function setSender($address, $name = null)
 154      {
 155          if (!is_array($address) && isset($name)) {
 156              $address = array($address => $name);
 157          }
 158  
 159          if (!$this->_setHeaderFieldModel('Sender', (array) $address)) {
 160              $this->getHeaders()->addMailboxHeader('Sender', (array) $address);
 161          }
 162  
 163          return $this;
 164      }
 165  
 166      /**
 167       * Get the sender of this message.
 168       *
 169       * @return string
 170       */
 171      public function getSender()
 172      {
 173          return $this->_getHeaderFieldModel('Sender');
 174      }
 175  
 176      /**
 177       * Add a From: address to this message.
 178       *
 179       * If $name is passed this name will be associated with the address.
 180       *
 181       * @param string $address
 182       * @param string $name    optional
 183       *
 184       * @return Swift_Mime_SimpleMessage
 185       */
 186      public function addFrom($address, $name = null)
 187      {
 188          $current = $this->getFrom();
 189          $current[$address] = $name;
 190  
 191          return $this->setFrom($current);
 192      }
 193  
 194      /**
 195       * Set the from address of this message.
 196       *
 197       * You may pass an array of addresses if this message is from multiple people.
 198       *
 199       * If $name is passed and the first parameter is a string, this name will be
 200       * associated with the address.
 201       *
 202       * @param string|array $addresses
 203       * @param string       $name      optional
 204       *
 205       * @return Swift_Mime_SimpleMessage
 206       */
 207      public function setFrom($addresses, $name = null)
 208      {
 209          if (!is_array($addresses) && isset($name)) {
 210              $addresses = array($addresses => $name);
 211          }
 212  
 213          if (!$this->_setHeaderFieldModel('From', (array) $addresses)) {
 214              $this->getHeaders()->addMailboxHeader('From', (array) $addresses);
 215          }
 216  
 217          return $this;
 218      }
 219  
 220      /**
 221       * Get the from address of this message.
 222       *
 223       * @return mixed
 224       */
 225      public function getFrom()
 226      {
 227          return $this->_getHeaderFieldModel('From');
 228      }
 229  
 230      /**
 231       * Add a Reply-To: address to this message.
 232       *
 233       * If $name is passed this name will be associated with the address.
 234       *
 235       * @param string $address
 236       * @param string $name    optional
 237       *
 238       * @return Swift_Mime_SimpleMessage
 239       */
 240      public function addReplyTo($address, $name = null)
 241      {
 242          $current = $this->getReplyTo();
 243          $current[$address] = $name;
 244  
 245          return $this->setReplyTo($current);
 246      }
 247  
 248      /**
 249       * Set the reply-to address of this message.
 250       *
 251       * You may pass an array of addresses if replies will go to multiple people.
 252       *
 253       * If $name is passed and the first parameter is a string, this name will be
 254       * associated with the address.
 255       *
 256       * @param string $addresses
 257       * @param string $name      optional
 258       *
 259       * @return Swift_Mime_SimpleMessage
 260       */
 261      public function setReplyTo($addresses, $name = null)
 262      {
 263          if (!is_array($addresses) && isset($name)) {
 264              $addresses = array($addresses => $name);
 265          }
 266  
 267          if (!$this->_setHeaderFieldModel('Reply-To', (array) $addresses)) {
 268              $this->getHeaders()->addMailboxHeader('Reply-To', (array) $addresses);
 269          }
 270  
 271          return $this;
 272      }
 273  
 274      /**
 275       * Get the reply-to address of this message.
 276       *
 277       * @return string
 278       */
 279      public function getReplyTo()
 280      {
 281          return $this->_getHeaderFieldModel('Reply-To');
 282      }
 283  
 284      /**
 285       * Add a To: address to this message.
 286       *
 287       * If $name is passed this name will be associated with the address.
 288       *
 289       * @param string $address
 290       * @param string $name    optional
 291       *
 292       * @return Swift_Mime_SimpleMessage
 293       */
 294      public function addTo($address, $name = null)
 295      {
 296          $current = $this->getTo();
 297          $current[$address] = $name;
 298  
 299          return $this->setTo($current);
 300      }
 301  
 302      /**
 303       * Set the to addresses of this message.
 304       *
 305       * If multiple recipients will receive the message an array should be used.
 306       * Example: array('receiver@domain.org', 'other@domain.org' => 'A name')
 307       *
 308       * If $name is passed and the first parameter is a string, this name will be
 309       * associated with the address.
 310       *
 311       * @param mixed  $addresses
 312       * @param string $name      optional
 313       *
 314       * @return Swift_Mime_SimpleMessage
 315       */
 316      public function setTo($addresses, $name = null)
 317      {
 318          if (!is_array($addresses) && isset($name)) {
 319              $addresses = array($addresses => $name);
 320          }
 321  
 322          if (!$this->_setHeaderFieldModel('To', (array) $addresses)) {
 323              $this->getHeaders()->addMailboxHeader('To', (array) $addresses);
 324          }
 325  
 326          return $this;
 327      }
 328  
 329      /**
 330       * Get the To addresses of this message.
 331       *
 332       * @return array
 333       */
 334      public function getTo()
 335      {
 336          return $this->_getHeaderFieldModel('To');
 337      }
 338  
 339      /**
 340       * Add a Cc: address to this message.
 341       *
 342       * If $name is passed this name will be associated with the address.
 343       *
 344       * @param string $address
 345       * @param string $name    optional
 346       *
 347       * @return Swift_Mime_SimpleMessage
 348       */
 349      public function addCc($address, $name = null)
 350      {
 351          $current = $this->getCc();
 352          $current[$address] = $name;
 353  
 354          return $this->setCc($current);
 355      }
 356  
 357      /**
 358       * Set the Cc addresses of this message.
 359       *
 360       * If $name is passed and the first parameter is a string, this name will be
 361       * associated with the address.
 362       *
 363       * @param mixed  $addresses
 364       * @param string $name      optional
 365       *
 366       * @return Swift_Mime_SimpleMessage
 367       */
 368      public function setCc($addresses, $name = null)
 369      {
 370          if (!is_array($addresses) && isset($name)) {
 371              $addresses = array($addresses => $name);
 372          }
 373  
 374          if (!$this->_setHeaderFieldModel('Cc', (array) $addresses)) {
 375              $this->getHeaders()->addMailboxHeader('Cc', (array) $addresses);
 376          }
 377  
 378          return $this;
 379      }
 380  
 381      /**
 382       * Get the Cc address of this message.
 383       *
 384       * @return array
 385       */
 386      public function getCc()
 387      {
 388          return $this->_getHeaderFieldModel('Cc');
 389      }
 390  
 391      /**
 392       * Add a Bcc: address to this message.
 393       *
 394       * If $name is passed this name will be associated with the address.
 395       *
 396       * @param string $address
 397       * @param string $name    optional
 398       *
 399       * @return Swift_Mime_SimpleMessage
 400       */
 401      public function addBcc($address, $name = null)
 402      {
 403          $current = $this->getBcc();
 404          $current[$address] = $name;
 405  
 406          return $this->setBcc($current);
 407      }
 408  
 409      /**
 410       * Set the Bcc addresses of this message.
 411       *
 412       * If $name is passed and the first parameter is a string, this name will be
 413       * associated with the address.
 414       *
 415       * @param mixed  $addresses
 416       * @param string $name      optional
 417       *
 418       * @return Swift_Mime_SimpleMessage
 419       */
 420      public function setBcc($addresses, $name = null)
 421      {
 422          if (!is_array($addresses) && isset($name)) {
 423              $addresses = array($addresses => $name);
 424          }
 425  
 426          if (!$this->_setHeaderFieldModel('Bcc', (array) $addresses)) {
 427              $this->getHeaders()->addMailboxHeader('Bcc', (array) $addresses);
 428          }
 429  
 430          return $this;
 431      }
 432  
 433      /**
 434       * Get the Bcc addresses of this message.
 435       *
 436       * @return array
 437       */
 438      public function getBcc()
 439      {
 440          return $this->_getHeaderFieldModel('Bcc');
 441      }
 442  
 443      /**
 444       * Set the priority of this message.
 445       *
 446       * The value is an integer where 1 is the highest priority and 5 is the lowest.
 447       *
 448       * @param int $priority
 449       *
 450       * @return Swift_Mime_SimpleMessage
 451       */
 452      public function setPriority($priority)
 453      {
 454          $priorityMap = array(
 455              1 => 'Highest',
 456              2 => 'High',
 457              3 => 'Normal',
 458              4 => 'Low',
 459              5 => 'Lowest',
 460              );
 461          $pMapKeys = array_keys($priorityMap);
 462          if ($priority > max($pMapKeys)) {
 463              $priority = max($pMapKeys);
 464          } elseif ($priority < min($pMapKeys)) {
 465              $priority = min($pMapKeys);
 466          }
 467          if (!$this->_setHeaderFieldModel('X-Priority',
 468              sprintf('%d (%s)', $priority, $priorityMap[$priority]))) {
 469              $this->getHeaders()->addTextHeader('X-Priority',
 470                  sprintf('%d (%s)', $priority, $priorityMap[$priority]));
 471          }
 472  
 473          return $this;
 474      }
 475  
 476      /**
 477       * Get the priority of this message.
 478       *
 479       * The returned value is an integer where 1 is the highest priority and 5
 480       * is the lowest.
 481       *
 482       * @return int
 483       */
 484      public function getPriority()
 485      {
 486          list($priority) = sscanf($this->_getHeaderFieldModel('X-Priority'),
 487              '%[1-5]'
 488              );
 489  
 490          return isset($priority) ? $priority : 3;
 491      }
 492  
 493      /**
 494       * Ask for a delivery receipt from the recipient to be sent to $addresses.
 495       *
 496       * @param array $addresses
 497       *
 498       * @return Swift_Mime_SimpleMessage
 499       */
 500      public function setReadReceiptTo($addresses)
 501      {
 502          if (!$this->_setHeaderFieldModel('Disposition-Notification-To', $addresses)) {
 503              $this->getHeaders()
 504                  ->addMailboxHeader('Disposition-Notification-To', $addresses);
 505          }
 506  
 507          return $this;
 508      }
 509  
 510      /**
 511       * Get the addresses to which a read-receipt will be sent.
 512       *
 513       * @return string
 514       */
 515      public function getReadReceiptTo()
 516      {
 517          return $this->_getHeaderFieldModel('Disposition-Notification-To');
 518      }
 519  
 520      /**
 521       * Attach a {@link Swift_Mime_MimeEntity} such as an Attachment or MimePart.
 522       *
 523       * @param Swift_Mime_MimeEntity $entity
 524       *
 525       * @return Swift_Mime_SimpleMessage
 526       */
 527      public function attach(Swift_Mime_MimeEntity $entity)
 528      {
 529          $this->setChildren(array_merge($this->getChildren(), array($entity)));
 530  
 531          return $this;
 532      }
 533  
 534      /**
 535       * Remove an already attached entity.
 536       *
 537       * @param Swift_Mime_MimeEntity $entity
 538       *
 539       * @return Swift_Mime_SimpleMessage
 540       */
 541      public function detach(Swift_Mime_MimeEntity $entity)
 542      {
 543          $newChildren = array();
 544          foreach ($this->getChildren() as $child) {
 545              if ($entity !== $child) {
 546                  $newChildren[] = $child;
 547              }
 548          }
 549          $this->setChildren($newChildren);
 550  
 551          return $this;
 552      }
 553  
 554      /**
 555       * Attach a {@link Swift_Mime_MimeEntity} and return it's CID source.
 556       * This method should be used when embedding images or other data in a message.
 557       *
 558       * @param Swift_Mime_MimeEntity $entity
 559       *
 560       * @return string
 561       */
 562      public function embed(Swift_Mime_MimeEntity $entity)
 563      {
 564          $this->attach($entity);
 565  
 566          return 'cid:'.$entity->getId();
 567      }
 568  
 569      /**
 570       * Get this message as a complete string.
 571       *
 572       * @return string
 573       */
 574      public function toString()
 575      {
 576          if (count($children = $this->getChildren()) > 0 && $this->getBody() != '') {
 577              $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
 578              $string = parent::toString();
 579              $this->setChildren($children);
 580          } else {
 581              $string = parent::toString();
 582          }
 583  
 584          return $string;
 585      }
 586  
 587      /**
 588       * Returns a string representation of this object.
 589       *
 590       * @see toString()
 591       *
 592       * @return string
 593       */
 594      public function __toString()
 595      {
 596          return $this->toString();
 597      }
 598  
 599      /**
 600       * Write this message to a {@link Swift_InputByteStream}.
 601       *
 602       * @param Swift_InputByteStream $is
 603       */
 604      public function toByteStream(Swift_InputByteStream $is)
 605      {
 606          if (count($children = $this->getChildren()) > 0 && $this->getBody() != '') {
 607              $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
 608              parent::toByteStream($is);
 609              $this->setChildren($children);
 610          } else {
 611              parent::toByteStream($is);
 612          }
 613      }
 614  
 615      /** @see Swift_Mime_SimpleMimeEntity::_getIdField() */
 616      protected function _getIdField()
 617      {
 618          return 'Message-ID';
 619      }
 620  
 621      /** Turn the body of this message into a child of itself if needed */
 622      protected function _becomeMimePart()
 623      {
 624          $part = new parent($this->getHeaders()->newInstance(), $this->getEncoder(),
 625              $this->_getCache(), $this->_getGrammar(), $this->_userCharset
 626              );
 627          $part->setContentType($this->_userContentType);
 628          $part->setBody($this->getBody());
 629          $part->setFormat($this->_userFormat);
 630          $part->setDelSp($this->_userDelSp);
 631          $part->_setNestingLevel($this->_getTopNestingLevel());
 632  
 633          return $part;
 634      }
 635  
 636      /** Get the highest nesting level nested inside this message */
 637      private function _getTopNestingLevel()
 638      {
 639          $highestLevel = $this->getNestingLevel();
 640          foreach ($this->getChildren() as $child) {
 641              $childLevel = $child->getNestingLevel();
 642              if ($highestLevel < $childLevel) {
 643                  $highestLevel = $childLevel;
 644              }
 645          }
 646  
 647          return $highestLevel;
 648      }
 649  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1