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