SimpleMessage.php 17KB

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