SimpleMessage.php 15KB

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