XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
SimpleMessage.php
Go to the documentation of this file.
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 //@require 'Swift/Mime/Message.php';
12 //@require 'Swift/Mime/MimePart.php';
13 //@require 'Swift/Mime/MimeEntity.php';
14 //@require 'Swift/Mime/HeaderSet.php';
15 //@require 'Swift/Mime/ContentEncoder.php';
16 
24  implements Swift_Mime_Message
25 {
26 
34  public function __construct(Swift_Mime_HeaderSet $headers,
35  Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, $charset = null)
36  {
37  parent::__construct($headers, $encoder, $cache, $charset);
38  $this->getHeaders()->defineOrdering(array(
39  'Return-Path',
40  'Sender',
41  'Message-ID',
42  'Date',
43  'Subject',
44  'From',
45  'Reply-To',
46  'To',
47  'Cc',
48  'Bcc',
49  'MIME-Version',
50  'Content-Type',
51  'Content-Transfer-Encoding'
52  ));
53  $this->getHeaders()->setAlwaysDisplayed(
54  array('Date', 'Message-ID', 'From')
55  );
56  $this->getHeaders()->addTextHeader('MIME-Version', '1.0');
57  $this->setDate(time());
58  $this->setId($this->getId());
59  $this->getHeaders()->addMailboxHeader('From');
60  }
61 
66  public function getNestingLevel()
67  {
68  return self::LEVEL_TOP;
69  }
70 
75  public function setSubject($subject)
76  {
77  if (!$this->_setHeaderFieldModel('Subject', $subject))
78  {
79  $this->getHeaders()->addTextHeader('Subject', $subject);
80  }
81  return $this;
82  }
83 
88  public function getSubject()
89  {
90  return $this->_getHeaderFieldModel('Subject');
91  }
92 
97  public function setDate($date)
98  {
99  if (!$this->_setHeaderFieldModel('Date', $date))
100  {
101  $this->getHeaders()->addDateHeader('Date', $date);
102  }
103  return $this;
104  }
105 
110  public function getDate()
111  {
112  return $this->_getHeaderFieldModel('Date');
113  }
114 
119  public function setReturnPath($address)
120  {
121  if (!$this->_setHeaderFieldModel('Return-Path', $address))
122  {
123  $this->getHeaders()->addPathHeader('Return-Path', $address);
124  }
125  return $this;
126  }
127 
132  public function getReturnPath()
133  {
134  return $this->_getHeaderFieldModel('Return-Path');
135  }
136 
143  public function setSender($address, $name = null)
144  {
145  if (!is_array($address) && isset($name))
146  {
147  $address = array($address => $name);
148  }
149 
150  if (!$this->_setHeaderFieldModel('Sender', (array) $address))
151  {
152  $this->getHeaders()->addMailboxHeader('Sender', (array) $address);
153  }
154  return $this;
155  }
156 
161  public function getSender()
162  {
163  return $this->_getHeaderFieldModel('Sender');
164  }
165 
174  public function addFrom($address, $name = null)
175  {
176  $current = $this->getFrom();
177  $current[$address] = $name;
178  return $this->setFrom($current);
179  }
180 
192  public function setFrom($addresses, $name = null)
193  {
194  if (!is_array($addresses) && isset($name))
195  {
196  $addresses = array($addresses => $name);
197  }
198 
199  if (!$this->_setHeaderFieldModel('From', (array) $addresses))
200  {
201  $this->getHeaders()->addMailboxHeader('From', (array) $addresses);
202  }
203  return $this;
204  }
205 
211  public function getFrom()
212  {
213  return $this->_getHeaderFieldModel('From');
214  }
215 
224  public function addReplyTo($address, $name = null)
225  {
226  $current = $this->getReplyTo();
227  $current[$address] = $name;
228  return $this->setReplyTo($current);
229  }
230 
242  public function setReplyTo($addresses, $name = null)
243  {
244  if (!is_array($addresses) && isset($name))
245  {
246  $addresses = array($addresses => $name);
247  }
248 
249  if (!$this->_setHeaderFieldModel('Reply-To', (array) $addresses))
250  {
251  $this->getHeaders()->addMailboxHeader('Reply-To', (array) $addresses);
252  }
253  return $this;
254  }
255 
261  public function getReplyTo()
262  {
263  return $this->_getHeaderFieldModel('Reply-To');
264  }
265 
274  public function addTo($address, $name = null)
275  {
276  $current = $this->getTo();
277  $current[$address] = $name;
278  return $this->setTo($current);
279  }
280 
292  public function setTo($addresses, $name = null)
293  {
294  if (!is_array($addresses) && isset($name))
295  {
296  $addresses = array($addresses => $name);
297  }
298 
299  if (!$this->_setHeaderFieldModel('To', (array) $addresses))
300  {
301  $this->getHeaders()->addMailboxHeader('To', (array) $addresses);
302  }
303  return $this;
304  }
305 
311  public function getTo()
312  {
313  return $this->_getHeaderFieldModel('To');
314  }
315 
324  public function addCc($address, $name = null)
325  {
326  $current = $this->getCc();
327  $current[$address] = $name;
328  return $this->setCc($current);
329  }
330 
340  public function setCc($addresses, $name = null)
341  {
342  if (!is_array($addresses) && isset($name))
343  {
344  $addresses = array($addresses => $name);
345  }
346 
347  if (!$this->_setHeaderFieldModel('Cc', (array) $addresses))
348  {
349  $this->getHeaders()->addMailboxHeader('Cc', (array) $addresses);
350  }
351  return $this;
352  }
353 
359  public function getCc()
360  {
361  return $this->_getHeaderFieldModel('Cc');
362  }
363 
372  public function addBcc($address, $name = null)
373  {
374  $current = $this->getBcc();
375  $current[$address] = $name;
376  return $this->setBcc($current);
377  }
378 
388  public function setBcc($addresses, $name = null)
389  {
390  if (!is_array($addresses) && isset($name))
391  {
392  $addresses = array($addresses => $name);
393  }
394 
395  if (!$this->_setHeaderFieldModel('Bcc', (array) $addresses))
396  {
397  $this->getHeaders()->addMailboxHeader('Bcc', (array) $addresses);
398  }
399  return $this;
400  }
401 
407  public function getBcc()
408  {
409  return $this->_getHeaderFieldModel('Bcc');
410  }
411 
417  public function setPriority($priority)
418  {
419  $priorityMap = array(
420  1 => 'Highest',
421  2 => 'High',
422  3 => 'Normal',
423  4 => 'Low',
424  5 => 'Lowest'
425  );
426  $pMapKeys = array_keys($priorityMap);
427  if ($priority > max($pMapKeys))
428  {
429  $priority = max($pMapKeys);
430  }
431  elseif ($priority < min($pMapKeys))
432  {
433  $priority = min($pMapKeys);
434  }
435  if (!$this->_setHeaderFieldModel('X-Priority',
436  sprintf('%d (%s)', $priority, $priorityMap[$priority])))
437  {
438  $this->getHeaders()->addTextHeader('X-Priority',
439  sprintf('%d (%s)', $priority, $priorityMap[$priority]));
440  }
441  return $this;
442  }
443 
450  public function getPriority()
451  {
452  list($priority) = sscanf($this->_getHeaderFieldModel('X-Priority'),
453  '%[1-5]'
454  );
455  return isset($priority) ? $priority : 3;
456  }
457 
462  public function setReadReceiptTo($addresses)
463  {
464  if (!$this->_setHeaderFieldModel('Disposition-Notification-To', $addresses))
465  {
466  $this->getHeaders()
467  ->addMailboxHeader('Disposition-Notification-To', $addresses);
468  }
469  return $this;
470  }
471 
476  public function getReadReceiptTo()
477  {
478  return $this->_getHeaderFieldModel('Disposition-Notification-To');
479  }
480 
485  public function attach(Swift_Mime_MimeEntity $entity)
486  {
487  $this->setChildren(array_merge($this->getChildren(), array($entity)));
488  return $this;
489  }
490 
495  public function detach(Swift_Mime_MimeEntity $entity)
496  {
497  $newChildren = array();
498  foreach ($this->getChildren() as $child)
499  {
500  if ($entity !== $child)
501  {
502  $newChildren[] = $child;
503  }
504  }
505  $this->setChildren($newChildren);
506  return $this;
507  }
508 
515  public function embed(Swift_Mime_MimeEntity $entity)
516  {
517  $this->attach($entity);
518  return 'cid:' . $entity->getId();
519  }
520 
525  public function toString()
526  {
527  if (count($children = $this->getChildren()) > 0 && $this->getBody() != '')
528  {
529  $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
530  $string = parent::toString();
531  $this->setChildren($children);
532  }
533  else
534  {
535  $string = parent::toString();
536  }
537  return $string;
538  }
539 
547  public function __toString()
548  {
549  return $this->toString();
550  }
551 
556  public function toByteStream(Swift_InputByteStream $is)
557  {
558  if (count($children = $this->getChildren()) > 0 && $this->getBody() != '')
559  {
560  $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
561  parent::toByteStream($is);
562  $this->setChildren($children);
563  }
564  else
565  {
566  parent::toByteStream($is);
567  }
568  }
569 
570  // -- Protected methods
571 
573  protected function _getIdField()
574  {
575  return 'Message-ID';
576  }
577 
578  // -- Private methods
579 
581  private function _becomeMimePart()
582  {
583  $part = new parent($this->getHeaders()->newInstance(), $this->getEncoder(),
584  $this->_getCache(), $this->_userCharset
585  );
586  $part->setContentType($this->_userContentType);
587  $part->setBody($this->getBody());
588  $part->setFormat($this->_userFormat);
589  $part->setDelSp($this->_userDelSp);
590  $part->_setNestingLevel($this->_getTopNestingLevel());
591  return $part;
592  }
593 
595  private function _getTopNestingLevel()
596  {
597  $highestLevel = $this->getNestingLevel();
598  foreach ($this->getChildren() as $child)
599  {
600  $childLevel = $child->getNestingLevel();
601  if ($highestLevel < $childLevel)
602  {
603  $highestLevel = $childLevel;
604  }
605  }
606  return $highestLevel;
607  }
608 
609 }