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