SimpleMessage.php 17KB

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