SimpleMessage.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. * @author Chris Corbyn
  13. */
  14. class Swift_Mime_SimpleMessage extends Swift_Mime_MimePart implements Swift_Mime_Message
  15. {
  16. /**
  17. * Create a new SimpleMessage with $headers, $encoder and $cache.
  18. *
  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. 'Received',
  31. 'DKIM-Signature',
  32. 'DomainKey-Signature',
  33. 'Sender',
  34. 'Message-ID',
  35. 'Date',
  36. 'Subject',
  37. 'From',
  38. 'Reply-To',
  39. 'To',
  40. 'Cc',
  41. 'Bcc',
  42. 'MIME-Version',
  43. 'Content-Type',
  44. 'Content-Transfer-Encoding'
  45. ));
  46. $this->getHeaders()->setAlwaysDisplayed(array('Date', 'Message-ID', 'From'));
  47. $this->getHeaders()->addTextHeader('MIME-Version', '1.0');
  48. $this->setDate(time());
  49. $this->setId($this->getId());
  50. $this->getHeaders()->addMailboxHeader('From');
  51. }
  52. /**
  53. * Always returns {@link LEVEL_TOP} for a message instance.
  54. *
  55. * @return int
  56. */
  57. public function getNestingLevel()
  58. {
  59. return self::LEVEL_TOP;
  60. }
  61. /**
  62. * Set the subject of this message.
  63. *
  64. * @param string $subject
  65. *
  66. * @return Swift_Mime_SimpleMessage
  67. */
  68. public function setSubject($subject)
  69. {
  70. if (!$this->_setHeaderFieldModel('Subject', $subject)) {
  71. $this->getHeaders()->addTextHeader('Subject', $subject);
  72. }
  73. return $this;
  74. }
  75. /**
  76. * Get the subject of this message.
  77. *
  78. * @return string
  79. */
  80. public function getSubject()
  81. {
  82. return $this->_getHeaderFieldModel('Subject');
  83. }
  84. /**
  85. * Set the date at which this message was created.
  86. *
  87. * @param int $date
  88. *
  89. * @return Swift_Mime_SimpleMessage
  90. */
  91. public function setDate($date)
  92. {
  93. if (!$this->_setHeaderFieldModel('Date', $date)) {
  94. $this->getHeaders()->addDateHeader('Date', $date);
  95. }
  96. return $this;
  97. }
  98. /**
  99. * Get the date at which this message was created.
  100. *
  101. * @return int
  102. */
  103. public function getDate()
  104. {
  105. return $this->_getHeaderFieldModel('Date');
  106. }
  107. /**
  108. * Set the return-path (the bounce address) of this message.
  109. *
  110. * @param string $address
  111. *
  112. * @return Swift_Mime_SimpleMessage
  113. */
  114. public function setReturnPath($address)
  115. {
  116. if (!$this->_setHeaderFieldModel('Return-Path', $address)) {
  117. $this->getHeaders()->addPathHeader('Return-Path', $address);
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Get the return-path (bounce address) of this message.
  123. *
  124. * @return string
  125. */
  126. public function getReturnPath()
  127. {
  128. return $this->_getHeaderFieldModel('Return-Path');
  129. }
  130. /**
  131. * Set the sender of this message.
  132. *
  133. * This does not override the From field, but it has a higher significance.
  134. *
  135. * @param string $address
  136. * @param string $name optional
  137. *
  138. * @return Swift_Mime_SimpleMessage
  139. */
  140. public function setSender($address, $name = null)
  141. {
  142. if (!is_array($address) && isset($name)) {
  143. $address = array($address => $name);
  144. }
  145. if (!$this->_setHeaderFieldModel('Sender', (array) $address)) {
  146. $this->getHeaders()->addMailboxHeader('Sender', (array) $address);
  147. }
  148. return $this;
  149. }
  150. /**
  151. * Get the sender of this message.
  152. *
  153. * @return string
  154. */
  155. public function getSender()
  156. {
  157. return $this->_getHeaderFieldModel('Sender');
  158. }
  159. /**
  160. * Add a From: address to this message.
  161. *
  162. * If $name is passed this name will be associated with the address.
  163. *
  164. * @param string $address
  165. * @param string $name optional
  166. *
  167. * @return Swift_Mime_SimpleMessage
  168. */
  169. public function addFrom($address, $name = null)
  170. {
  171. $current = $this->getFrom();
  172. $current[$address] = $name;
  173. return $this->setFrom($current);
  174. }
  175. /**
  176. * Set the from address of this message.
  177. *
  178. * You may pass an array of addresses if this message is from multiple people.
  179. *
  180. * If $name is passed and the first parameter is a string, this name will be
  181. * associated with the address.
  182. *
  183. * @param string $addresses
  184. * @param string $name optional
  185. *
  186. * @return Swift_Mime_SimpleMessage
  187. */
  188. public function setFrom($addresses, $name = null)
  189. {
  190. if (!is_array($addresses) && isset($name)) {
  191. $addresses = array($addresses => $name);
  192. }
  193. if (!$this->_setHeaderFieldModel('From', (array) $addresses)) {
  194. $this->getHeaders()->addMailboxHeader('From', (array) $addresses);
  195. }
  196. return $this;
  197. }
  198. /**
  199. * Get the from address of this message.
  200. *
  201. * @return mixed
  202. */
  203. public function getFrom()
  204. {
  205. return $this->_getHeaderFieldModel('From');
  206. }
  207. /**
  208. * Add a Reply-To: address to this message.
  209. *
  210. * If $name is passed this name will be associated with the address.
  211. *
  212. * @param string $address
  213. * @param string $name optional
  214. *
  215. * @return Swift_Mime_SimpleMessage
  216. */
  217. public function addReplyTo($address, $name = null)
  218. {
  219. $current = $this->getReplyTo();
  220. $current[$address] = $name;
  221. return $this->setReplyTo($current);
  222. }
  223. /**
  224. * Set the reply-to address of this message.
  225. *
  226. * You may pass an array of addresses if replies will go to multiple people.
  227. *
  228. * If $name is passed and the first parameter is a string, this name will be
  229. * associated with the address.
  230. *
  231. * @param string $addresses
  232. * @param string $name optional
  233. *
  234. * @return Swift_Mime_SimpleMessage
  235. */
  236. public function setReplyTo($addresses, $name = null)
  237. {
  238. if (!is_array($addresses) && isset($name)) {
  239. $addresses = array($addresses => $name);
  240. }
  241. if (!$this->_setHeaderFieldModel('Reply-To', (array) $addresses)) {
  242. $this->getHeaders()->addMailboxHeader('Reply-To', (array) $addresses);
  243. }
  244. return $this;
  245. }
  246. /**
  247. * Get the reply-to address of this message.
  248. *
  249. * @return string
  250. */
  251. public function getReplyTo()
  252. {
  253. return $this->_getHeaderFieldModel('Reply-To');
  254. }
  255. /**
  256. * Add a To: address to this message.
  257. *
  258. * If $name is passed this name will be associated with the address.
  259. *
  260. * @param string $address
  261. * @param string $name optional
  262. *
  263. * @return Swift_Mime_SimpleMessage
  264. */
  265. public function addTo($address, $name = null)
  266. {
  267. $current = $this->getTo();
  268. $current[$address] = $name;
  269. return $this->setTo($current);
  270. }
  271. /**
  272. * Set the to addresses of this message.
  273. *
  274. * If multiple recipients will receive the message an array should be used.
  275. * Example: array('receiver@domain.org', 'other@domain.org' => 'A name')
  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 int $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 int
  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. /** @see Swift_Mime_SimpleMimeEntity::_getIdField() */
  553. protected function _getIdField()
  554. {
  555. return 'Message-ID';
  556. }
  557. /** Turn the body of this message into a child of itself if needed */
  558. protected function _becomeMimePart()
  559. {
  560. $part = new parent($this->getHeaders()->newInstance(), $this->getEncoder(),
  561. $this->_getCache(), $this->_getGrammar(), $this->_userCharset
  562. );
  563. $part->setContentType($this->_userContentType);
  564. $part->setBody($this->getBody());
  565. $part->setFormat($this->_userFormat);
  566. $part->setDelSp($this->_userDelSp);
  567. $part->_setNestingLevel($this->_getTopNestingLevel());
  568. return $part;
  569. }
  570. /** Get the highest nesting level nested inside this message */
  571. private function _getTopNestingLevel()
  572. {
  573. $highestLevel = $this->getNestingLevel();
  574. foreach ($this->getChildren() as $child) {
  575. $childLevel = $child->getNestingLevel();
  576. if ($highestLevel < $childLevel) {
  577. $highestLevel = $childLevel;
  578. }
  579. }
  580. return $highestLevel;
  581. }
  582. }