TransportBuilder.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /**
  3. * Mail Template Transport Builder
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Mail\Template;
  9. use Magento\Framework\App\TemplateTypesInterface;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Framework\Mail\MessageInterface;
  12. use Magento\Framework\Mail\MessageInterfaceFactory;
  13. use Magento\Framework\Mail\TransportInterfaceFactory;
  14. use Magento\Framework\ObjectManagerInterface;
  15. use Magento\Framework\Phrase;
  16. /**
  17. * TransportBuilder
  18. *
  19. * @api
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. * @since 100.0.2
  22. */
  23. class TransportBuilder
  24. {
  25. /**
  26. * Template Identifier
  27. *
  28. * @var string
  29. */
  30. protected $templateIdentifier;
  31. /**
  32. * Template Model
  33. *
  34. * @var string
  35. */
  36. protected $templateModel;
  37. /**
  38. * Template Variables
  39. *
  40. * @var array
  41. */
  42. protected $templateVars;
  43. /**
  44. * Template Options
  45. *
  46. * @var array
  47. */
  48. protected $templateOptions;
  49. /**
  50. * Mail Transport
  51. *
  52. * @var \Magento\Framework\Mail\TransportInterface
  53. */
  54. protected $transport;
  55. /**
  56. * Template Factory
  57. *
  58. * @var FactoryInterface
  59. */
  60. protected $templateFactory;
  61. /**
  62. * Object Manager
  63. *
  64. * @var \Magento\Framework\ObjectManagerInterface
  65. */
  66. protected $objectManager;
  67. /**
  68. * Message
  69. *
  70. * @var \Magento\Framework\Mail\Message
  71. */
  72. protected $message;
  73. /**
  74. * Sender resolver
  75. *
  76. * @var \Magento\Framework\Mail\Template\SenderResolverInterface
  77. */
  78. protected $_senderResolver;
  79. /**
  80. * @var \Magento\Framework\Mail\TransportInterfaceFactory
  81. */
  82. protected $mailTransportFactory;
  83. /**
  84. * @var \Magento\Framework\Mail\MessageInterfaceFactory
  85. */
  86. private $messageFactory;
  87. /**
  88. * @param FactoryInterface $templateFactory
  89. * @param MessageInterface $message
  90. * @param SenderResolverInterface $senderResolver
  91. * @param ObjectManagerInterface $objectManager
  92. * @param TransportInterfaceFactory $mailTransportFactory
  93. * @param MessageInterfaceFactory $messageFactory
  94. *
  95. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  96. */
  97. public function __construct(
  98. FactoryInterface $templateFactory,
  99. MessageInterface $message,
  100. SenderResolverInterface $senderResolver,
  101. ObjectManagerInterface $objectManager,
  102. TransportInterfaceFactory $mailTransportFactory,
  103. MessageInterfaceFactory $messageFactory = null
  104. ) {
  105. $this->templateFactory = $templateFactory;
  106. $this->objectManager = $objectManager;
  107. $this->_senderResolver = $senderResolver;
  108. $this->mailTransportFactory = $mailTransportFactory;
  109. $this->messageFactory = $messageFactory ?: $this->objectManager->get(MessageInterfaceFactory::class);
  110. $this->message = $this->messageFactory->create();
  111. }
  112. /**
  113. * Add cc address
  114. *
  115. * @param array|string $address
  116. * @param string $name
  117. * @return $this
  118. */
  119. public function addCc($address, $name = '')
  120. {
  121. $this->message->addCc($address, $name);
  122. return $this;
  123. }
  124. /**
  125. * Add to address
  126. *
  127. * @param array|string $address
  128. * @param string $name
  129. * @return $this
  130. */
  131. public function addTo($address, $name = '')
  132. {
  133. $this->message->addTo($address, $name);
  134. return $this;
  135. }
  136. /**
  137. * Add bcc address
  138. *
  139. * @param array|string $address
  140. * @return $this
  141. */
  142. public function addBcc($address)
  143. {
  144. $this->message->addBcc($address);
  145. return $this;
  146. }
  147. /**
  148. * Set Reply-To Header
  149. *
  150. * @param string $email
  151. * @param string|null $name
  152. * @return $this
  153. */
  154. public function setReplyTo($email, $name = null)
  155. {
  156. $this->message->setReplyTo($email, $name);
  157. return $this;
  158. }
  159. /**
  160. * Set mail from address
  161. *
  162. * @deprecated 102.0.1 This function sets the from address but does not provide
  163. * a way of setting the correct from addresses based on the scope.
  164. * @see setFromByScope()
  165. *
  166. * @param string|array $from
  167. * @return $this
  168. * @throws \Magento\Framework\Exception\MailException
  169. */
  170. public function setFrom($from)
  171. {
  172. return $this->setFromByScope($from, null);
  173. }
  174. /**
  175. * Set mail from address by scopeId
  176. *
  177. * @param string|array $from
  178. * @param string|int $scopeId
  179. * @return $this
  180. * @throws \Magento\Framework\Exception\MailException
  181. * @since 102.0.1
  182. */
  183. public function setFromByScope($from, $scopeId = null)
  184. {
  185. $result = $this->_senderResolver->resolve($from, $scopeId);
  186. $this->message->setFromAddress($result['email'], $result['name']);
  187. return $this;
  188. }
  189. /**
  190. * Set template identifier
  191. *
  192. * @param string $templateIdentifier
  193. * @return $this
  194. */
  195. public function setTemplateIdentifier($templateIdentifier)
  196. {
  197. $this->templateIdentifier = $templateIdentifier;
  198. return $this;
  199. }
  200. /**
  201. * Set template model
  202. *
  203. * @param string $templateModel
  204. * @return $this
  205. */
  206. public function setTemplateModel($templateModel)
  207. {
  208. $this->templateModel = $templateModel;
  209. return $this;
  210. }
  211. /**
  212. * Set template vars
  213. *
  214. * @param array $templateVars
  215. * @return $this
  216. */
  217. public function setTemplateVars($templateVars)
  218. {
  219. $this->templateVars = $templateVars;
  220. return $this;
  221. }
  222. /**
  223. * Set template options
  224. *
  225. * @param array $templateOptions
  226. * @return $this
  227. */
  228. public function setTemplateOptions($templateOptions)
  229. {
  230. $this->templateOptions = $templateOptions;
  231. return $this;
  232. }
  233. /**
  234. * Get mail transport
  235. *
  236. * @return \Magento\Framework\Mail\TransportInterface
  237. * @throws LocalizedException
  238. */
  239. public function getTransport()
  240. {
  241. $this->prepareMessage();
  242. $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]);
  243. $this->reset();
  244. return $mailTransport;
  245. }
  246. /**
  247. * Reset object state
  248. *
  249. * @return $this
  250. */
  251. protected function reset()
  252. {
  253. $this->message = $this->messageFactory->create();
  254. $this->templateIdentifier = null;
  255. $this->templateVars = null;
  256. $this->templateOptions = null;
  257. return $this;
  258. }
  259. /**
  260. * Get template
  261. *
  262. * @return \Magento\Framework\Mail\TemplateInterface
  263. */
  264. protected function getTemplate()
  265. {
  266. return $this->templateFactory->get($this->templateIdentifier, $this->templateModel)
  267. ->setVars($this->templateVars)
  268. ->setOptions($this->templateOptions);
  269. }
  270. /**
  271. * Prepare message.
  272. *
  273. * @return $this
  274. * @throws LocalizedException if template type is unknown
  275. */
  276. protected function prepareMessage()
  277. {
  278. $template = $this->getTemplate();
  279. $body = $template->processTemplate();
  280. switch ($template->getType()) {
  281. case TemplateTypesInterface::TYPE_TEXT:
  282. $this->message->setBodyText($body);
  283. break;
  284. case TemplateTypesInterface::TYPE_HTML:
  285. $this->message->setBodyHtml($body);
  286. break;
  287. default:
  288. throw new LocalizedException(
  289. new Phrase('Unknown template type')
  290. );
  291. }
  292. $this->message->setSubject(html_entity_decode($template->getSubject(), ENT_QUOTES));
  293. return $this;
  294. }
  295. }