WrappedTemplatedEmail.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\Bridge\Twig\Mime;
  11. use Symfony\Component\Mime\Address;
  12. use Twig\Environment;
  13. /**
  14. * @internal
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. final class WrappedTemplatedEmail
  19. {
  20. private $twig;
  21. private $message;
  22. public function __construct(Environment $twig, TemplatedEmail $message)
  23. {
  24. $this->twig = $twig;
  25. $this->message = $message;
  26. }
  27. public function toName(): string
  28. {
  29. return $this->message->getTo()[0]->getName();
  30. }
  31. public function image(string $image, string $contentType = null): string
  32. {
  33. $file = $this->twig->getLoader()->getSourceContext($image);
  34. if ($path = $file->getPath()) {
  35. $this->message->embedFromPath($path, $image, $contentType);
  36. } else {
  37. $this->message->embed($file->getCode(), $image, $contentType);
  38. }
  39. return 'cid:'.$image;
  40. }
  41. public function attach(string $file, string $name = null, string $contentType = null): void
  42. {
  43. $file = $this->twig->getLoader()->getSourceContext($file);
  44. if ($path = $file->getPath()) {
  45. $this->message->attachFromPath($path, $name, $contentType);
  46. } else {
  47. $this->message->attach($file->getCode(), $name, $contentType);
  48. }
  49. }
  50. /**
  51. * @return $this
  52. */
  53. public function setSubject(string $subject): self
  54. {
  55. $this->message->subject($subject);
  56. return $this;
  57. }
  58. public function getSubject(): ?string
  59. {
  60. return $this->message->getSubject();
  61. }
  62. /**
  63. * @return $this
  64. */
  65. public function setReturnPath(string $address): self
  66. {
  67. $this->message->returnPath($address);
  68. return $this;
  69. }
  70. public function getReturnPath(): string
  71. {
  72. return $this->message->getReturnPath();
  73. }
  74. /**
  75. * @return $this
  76. */
  77. public function addFrom(string $address, string $name = ''): self
  78. {
  79. $this->message->addFrom(new Address($address, $name));
  80. return $this;
  81. }
  82. /**
  83. * @return Address[]
  84. */
  85. public function getFrom(): array
  86. {
  87. return $this->message->getFrom();
  88. }
  89. /**
  90. * @return $this
  91. */
  92. public function addReplyTo(string $address): self
  93. {
  94. $this->message->addReplyTo($address);
  95. return $this;
  96. }
  97. /**
  98. * @return Address[]
  99. */
  100. public function getReplyTo(): array
  101. {
  102. return $this->message->getReplyTo();
  103. }
  104. /**
  105. * @return $this
  106. */
  107. public function addTo(string $address, string $name = ''): self
  108. {
  109. $this->message->addTo(new Address($address, $name));
  110. return $this;
  111. }
  112. /**
  113. * @return Address[]
  114. */
  115. public function getTo(): array
  116. {
  117. return $this->message->getTo();
  118. }
  119. /**
  120. * @return $this
  121. */
  122. public function addCc(string $address, string $name = ''): self
  123. {
  124. $this->message->addCc(new Address($address, $name));
  125. return $this;
  126. }
  127. /**
  128. * @return Address[]
  129. */
  130. public function getCc(): array
  131. {
  132. return $this->message->getCc();
  133. }
  134. /**
  135. * @return $this
  136. */
  137. public function addBcc(string $address, string $name = ''): self
  138. {
  139. $this->message->addBcc(new Address($address, $name));
  140. return $this;
  141. }
  142. /**
  143. * @return Address[]
  144. */
  145. public function getBcc(): array
  146. {
  147. return $this->message->getBcc();
  148. }
  149. /**
  150. * @return $this
  151. */
  152. public function setPriority(int $priority): self
  153. {
  154. $this->message->setPriority($priority);
  155. return $this;
  156. }
  157. public function getPriority(): int
  158. {
  159. return $this->message->getPriority();
  160. }
  161. }