SimpleMessage.php 16 KB

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