Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Email.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime;
  11. use Symfony\Component\Mime\Exception\LogicException;
  12. use Symfony\Component\Mime\Part\AbstractPart;
  13. use Symfony\Component\Mime\Part\DataPart;
  14. use Symfony\Component\Mime\Part\Multipart\AlternativePart;
  15. use Symfony\Component\Mime\Part\Multipart\MixedPart;
  16. use Symfony\Component\Mime\Part\Multipart\RelatedPart;
  17. use Symfony\Component\Mime\Part\TextPart;
  18. /**
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. *
  21. * @experimental in 4.3
  22. */
  23. class Email extends Message
  24. {
  25. const PRIORITY_HIGHEST = 1;
  26. const PRIORITY_HIGH = 2;
  27. const PRIORITY_NORMAL = 3;
  28. const PRIORITY_LOW = 4;
  29. const PRIORITY_LOWEST = 5;
  30. private const PRIORITY_MAP = [
  31. self::PRIORITY_HIGHEST => 'Highest',
  32. self::PRIORITY_HIGH => 'High',
  33. self::PRIORITY_NORMAL => 'Normal',
  34. self::PRIORITY_LOW => 'Low',
  35. self::PRIORITY_LOWEST => 'Lowest',
  36. ];
  37. private $text;
  38. private $textCharset;
  39. private $html;
  40. private $htmlCharset;
  41. private $attachments = [];
  42. /**
  43. * @return $this
  44. */
  45. public function subject(string $subject)
  46. {
  47. return $this->setHeaderBody('Text', 'Subject', $subject);
  48. }
  49. public function getSubject(): ?string
  50. {
  51. return $this->getHeaders()->getHeaderBody('Subject');
  52. }
  53. /**
  54. * @return $this
  55. */
  56. public function date(\DateTimeInterface $dateTime)
  57. {
  58. return $this->setHeaderBody('Date', 'Date', $dateTime);
  59. }
  60. public function getDate(): ?\DateTimeImmutable
  61. {
  62. return $this->getHeaders()->getHeaderBody('Date');
  63. }
  64. /**
  65. * @param Address|string $address
  66. *
  67. * @return $this
  68. */
  69. public function returnPath($address)
  70. {
  71. return $this->setHeaderBody('Path', 'Return-Path', Address::create($address));
  72. }
  73. public function getReturnPath(): ?Address
  74. {
  75. return $this->getHeaders()->getHeaderBody('Return-Path');
  76. }
  77. /**
  78. * @param Address|string $address
  79. *
  80. * @return $this
  81. */
  82. public function sender($address)
  83. {
  84. return $this->setHeaderBody('Mailbox', 'Sender', Address::create($address));
  85. }
  86. public function getSender(): ?Address
  87. {
  88. return $this->getHeaders()->getHeaderBody('Sender');
  89. }
  90. /**
  91. * @param Address|NamedAddress|string ...$addresses
  92. *
  93. * @return $this
  94. */
  95. public function addFrom(...$addresses)
  96. {
  97. return $this->addListAddressHeaderBody('From', $addresses);
  98. }
  99. /**
  100. * @param Address|NamedAddress|string ...$addresses
  101. *
  102. * @return $this
  103. */
  104. public function from(...$addresses)
  105. {
  106. return $this->setListAddressHeaderBody('From', $addresses);
  107. }
  108. /**
  109. * @return (Address|NamedAddress)[]
  110. */
  111. public function getFrom(): array
  112. {
  113. return $this->getHeaders()->getHeaderBody('From') ?: [];
  114. }
  115. /**
  116. * @param Address|string ...$addresses
  117. *
  118. * @return $this
  119. */
  120. public function addReplyTo(...$addresses)
  121. {
  122. return $this->addListAddressHeaderBody('Reply-To', $addresses);
  123. }
  124. /**
  125. * @param Address|string ...$addresses
  126. *
  127. * @return $this
  128. */
  129. public function replyTo(...$addresses)
  130. {
  131. return $this->setListAddressHeaderBody('Reply-To', $addresses);
  132. }
  133. /**
  134. * @return Address[]
  135. */
  136. public function getReplyTo(): array
  137. {
  138. return $this->getHeaders()->getHeaderBody('Reply-To') ?: [];
  139. }
  140. /**
  141. * @param Address|NamedAddress|string ...$addresses
  142. *
  143. * @return $this
  144. */
  145. public function addTo(...$addresses)
  146. {
  147. return $this->addListAddressHeaderBody('To', $addresses);
  148. }
  149. /**
  150. * @param Address|NamedAddress|string ...$addresses
  151. *
  152. * @return $this
  153. */
  154. public function to(...$addresses)
  155. {
  156. return $this->setListAddressHeaderBody('To', $addresses);
  157. }
  158. /**
  159. * @return (Address|NamedAddress)[]
  160. */
  161. public function getTo(): array
  162. {
  163. return $this->getHeaders()->getHeaderBody('To') ?: [];
  164. }
  165. /**
  166. * @param Address|NamedAddress|string ...$addresses
  167. *
  168. * @return $this
  169. */
  170. public function addCc(...$addresses)
  171. {
  172. return $this->addListAddressHeaderBody('Cc', $addresses);
  173. }
  174. /**
  175. * @param Address|string ...$addresses
  176. *
  177. * @return $this
  178. */
  179. public function cc(...$addresses)
  180. {
  181. return $this->setListAddressHeaderBody('Cc', $addresses);
  182. }
  183. /**
  184. * @return (Address|NamedAddress)[]
  185. */
  186. public function getCc(): array
  187. {
  188. return $this->getHeaders()->getHeaderBody('Cc') ?: [];
  189. }
  190. /**
  191. * @param Address|NamedAddress|string ...$addresses
  192. *
  193. * @return $this
  194. */
  195. public function addBcc(...$addresses)
  196. {
  197. return $this->addListAddressHeaderBody('Bcc', $addresses);
  198. }
  199. /**
  200. * @param Address|string ...$addresses
  201. *
  202. * @return $this
  203. */
  204. public function bcc(...$addresses)
  205. {
  206. return $this->setListAddressHeaderBody('Bcc', $addresses);
  207. }
  208. /**
  209. * @return (Address|NamedAddress)[]
  210. */
  211. public function getBcc(): array
  212. {
  213. return $this->getHeaders()->getHeaderBody('Bcc') ?: [];
  214. }
  215. /**
  216. * Sets the priority of this message.
  217. *
  218. * The value is an integer where 1 is the highest priority and 5 is the lowest.
  219. *
  220. * @return $this
  221. */
  222. public function priority(int $priority)
  223. {
  224. if ($priority > 5) {
  225. $priority = 5;
  226. } elseif ($priority < 1) {
  227. $priority = 1;
  228. }
  229. return $this->setHeaderBody('Text', 'X-Priority', sprintf('%d (%s)', $priority, self::PRIORITY_MAP[$priority]));
  230. }
  231. /**
  232. * Get the priority of this message.
  233. *
  234. * The returned value is an integer where 1 is the highest priority and 5
  235. * is the lowest.
  236. */
  237. public function getPriority(): int
  238. {
  239. list($priority) = sscanf($this->getHeaders()->getHeaderBody('X-Priority'), '%[1-5]');
  240. return $priority ?? 3;
  241. }
  242. /**
  243. * @param resource|string $body
  244. *
  245. * @return $this
  246. */
  247. public function text($body, string $charset = 'utf-8')
  248. {
  249. $this->text = $body;
  250. $this->textCharset = $charset;
  251. return $this;
  252. }
  253. /**
  254. * @return resource|string|null
  255. */
  256. public function getTextBody()
  257. {
  258. return $this->text;
  259. }
  260. public function getTextCharset(): ?string
  261. {
  262. return $this->textCharset;
  263. }
  264. /**
  265. * @param resource|string|null $body
  266. *
  267. * @return $this
  268. */
  269. public function html($body, string $charset = 'utf-8')
  270. {
  271. $this->html = $body;
  272. $this->htmlCharset = $charset;
  273. return $this;
  274. }
  275. /**
  276. * @return resource|string|null
  277. */
  278. public function getHtmlBody()
  279. {
  280. return $this->html;
  281. }
  282. public function getHtmlCharset(): ?string
  283. {
  284. return $this->htmlCharset;
  285. }
  286. /**
  287. * @param resource|string $body
  288. *
  289. * @return $this
  290. */
  291. public function attach($body, string $name = null, string $contentType = null)
  292. {
  293. $this->attachments[] = ['body' => $body, 'name' => $name, 'content-type' => $contentType, 'inline' => false];
  294. return $this;
  295. }
  296. /**
  297. * @return $this
  298. */
  299. public function attachFromPath(string $path, string $name = null, string $contentType = null)
  300. {
  301. $this->attachments[] = ['path' => $path, 'name' => $name, 'content-type' => $contentType, 'inline' => false];
  302. return $this;
  303. }
  304. /**
  305. * @param resource|string $body
  306. *
  307. * @return $this
  308. */
  309. public function embed($body, string $name = null, string $contentType = null)
  310. {
  311. $this->attachments[] = ['body' => $body, 'name' => $name, 'content-type' => $contentType, 'inline' => true];
  312. return $this;
  313. }
  314. /**
  315. * @return $this
  316. */
  317. public function embedFromPath(string $path, string $name = null, string $contentType = null)
  318. {
  319. $this->attachments[] = ['path' => $path, 'name' => $name, 'content-type' => $contentType, 'inline' => true];
  320. return $this;
  321. }
  322. /**
  323. * @return $this
  324. */
  325. public function attachPart(DataPart $part)
  326. {
  327. $this->attachments[] = ['part' => $part];
  328. return $this;
  329. }
  330. /**
  331. * @return DataPart[]
  332. */
  333. public function getAttachments(): array
  334. {
  335. $parts = [];
  336. foreach ($this->attachments as $attachment) {
  337. $parts[] = $this->createDataPart($attachment);
  338. }
  339. return $parts;
  340. }
  341. public function getBody(): AbstractPart
  342. {
  343. if (null !== $body = parent::getBody()) {
  344. return $body;
  345. }
  346. return $this->generateBody();
  347. }
  348. /**
  349. * Generates an AbstractPart based on the raw body of a message.
  350. *
  351. * The most "complex" part generated by this method is when there is text and HTML bodies
  352. * with related images for the HTML part and some attachments:
  353. *
  354. * multipart/mixed
  355. * |
  356. * |------------> multipart/related
  357. * | |
  358. * | |------------> multipart/alternative
  359. * | | |
  360. * | | ------------> text/plain (with content)
  361. * | | |
  362. * | | ------------> text/html (with content)
  363. * | |
  364. * | ------------> image/png (with content)
  365. * |
  366. * ------------> application/pdf (with content)
  367. */
  368. private function generateBody(): AbstractPart
  369. {
  370. if (null === $this->text && null === $this->html) {
  371. throw new LogicException('A message must have a text and/or an HTML part.');
  372. }
  373. $part = null === $this->text ? null : new TextPart($this->text, $this->textCharset);
  374. [$htmlPart, $attachmentParts, $inlineParts] = $this->prepareParts();
  375. if (null !== $htmlPart) {
  376. if (null !== $part) {
  377. $part = new AlternativePart($part, $htmlPart);
  378. } else {
  379. $part = $htmlPart;
  380. }
  381. }
  382. if ($inlineParts) {
  383. $part = new RelatedPart($part, ...$inlineParts);
  384. }
  385. if ($attachmentParts) {
  386. $part = new MixedPart($part, ...$attachmentParts);
  387. }
  388. return $part;
  389. }
  390. private function prepareParts(): ?array
  391. {
  392. $names = [];
  393. $htmlPart = null;
  394. $html = $this->html;
  395. if (null !== $this->html) {
  396. if (\is_resource($html)) {
  397. if (stream_get_meta_data($html)['seekable'] ?? false) {
  398. rewind($html);
  399. }
  400. $html = stream_get_contents($html);
  401. }
  402. $htmlPart = new TextPart($html, $this->htmlCharset, 'html');
  403. preg_match_all('(<img\s+[^>]*src\s*=\s*(?:([\'"])cid:([^"]+)\\1|cid:([^>\s]+)))i', $html, $names);
  404. $names = array_filter(array_unique(array_merge($names[2], $names[3])));
  405. }
  406. $attachmentParts = $inlineParts = [];
  407. foreach ($this->attachments as $attachment) {
  408. foreach ($names as $name) {
  409. if (isset($attachment['part'])) {
  410. continue;
  411. }
  412. if ($name !== $attachment['name']) {
  413. continue;
  414. }
  415. if (isset($inlineParts[$name])) {
  416. continue 2;
  417. }
  418. $attachment['inline'] = true;
  419. $inlineParts[$name] = $part = $this->createDataPart($attachment);
  420. $html = str_replace('cid:'.$name, 'cid:'.$part->getContentId(), $html);
  421. continue 2;
  422. }
  423. $attachmentParts[] = $this->createDataPart($attachment);
  424. }
  425. if (null !== $htmlPart) {
  426. $htmlPart = new TextPart($html, $this->htmlCharset, 'html');
  427. }
  428. return [$htmlPart, $attachmentParts, array_values($inlineParts)];
  429. }
  430. private function createDataPart(array $attachment): DataPart
  431. {
  432. if (isset($attachment['part'])) {
  433. return $attachment['part'];
  434. }
  435. if (isset($attachment['body'])) {
  436. $part = new DataPart($attachment['body'], $attachment['name'] ?? null, $attachment['content-type'] ?? null);
  437. } else {
  438. $part = DataPart::fromPath($attachment['path'] ?? '', $attachment['name'] ?? null, $attachment['content-type'] ?? null);
  439. }
  440. if ($attachment['inline']) {
  441. $part->asInline();
  442. }
  443. return $part;
  444. }
  445. /**
  446. * @return $this
  447. */
  448. private function setHeaderBody(string $type, string $name, $body)
  449. {
  450. $this->getHeaders()->setHeaderBody($type, $name, $body);
  451. return $this;
  452. }
  453. private function addListAddressHeaderBody($name, array $addresses)
  454. {
  455. if (!$to = $this->getHeaders()->get($name)) {
  456. return $this->setListAddressHeaderBody($name, $addresses);
  457. }
  458. $to->addAddresses(Address::createArray($addresses));
  459. return $this;
  460. }
  461. private function setListAddressHeaderBody($name, array $addresses)
  462. {
  463. $addresses = Address::createArray($addresses);
  464. $headers = $this->getHeaders();
  465. if ($to = $headers->get($name)) {
  466. $to->setAddresses($addresses);
  467. } else {
  468. $headers->addMailboxListHeader($name, $addresses);
  469. }
  470. return $this;
  471. }
  472. /**
  473. * @internal
  474. */
  475. public function __serialize(): array
  476. {
  477. if (\is_resource($this->text)) {
  478. if (stream_get_meta_data($this->text)['seekable'] ?? false) {
  479. rewind($this->text);
  480. }
  481. $this->text = stream_get_contents($this->text);
  482. }
  483. if (\is_resource($this->html)) {
  484. if (stream_get_meta_data($this->html)['seekable'] ?? false) {
  485. rewind($this->html);
  486. }
  487. $this->html = stream_get_contents($this->html);
  488. }
  489. foreach ($this->attachments as $i => $attachment) {
  490. if (isset($attachment['body']) && \is_resource($attachment['body'])) {
  491. if (stream_get_meta_data($attachment['body'])['seekable'] ?? false) {
  492. rewind($attachment['body']);
  493. }
  494. $this->attachments[$i]['body'] = stream_get_contents($attachment['body']);
  495. }
  496. }
  497. return [$this->text, $this->textCharset, $this->html, $this->htmlCharset, $this->attachments, parent::__serialize()];
  498. }
  499. /**
  500. * @internal
  501. */
  502. public function __unserialize(array $data): void
  503. {
  504. [$this->text, $this->textCharset, $this->html, $this->htmlCharset, $this->attachments, $parentData] = $data;
  505. parent::__unserialize($parentData);
  506. }
  507. }