123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- /**
- * Mail Template Transport Builder
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Mail\Template;
- use Magento\Framework\App\TemplateTypesInterface;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Framework\Mail\MessageInterface;
- use Magento\Framework\Mail\MessageInterfaceFactory;
- use Magento\Framework\Mail\TransportInterfaceFactory;
- use Magento\Framework\ObjectManagerInterface;
- use Magento\Framework\Phrase;
- /**
- * TransportBuilder
- *
- * @api
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class TransportBuilder
- {
- /**
- * Template Identifier
- *
- * @var string
- */
- protected $templateIdentifier;
- /**
- * Template Model
- *
- * @var string
- */
- protected $templateModel;
- /**
- * Template Variables
- *
- * @var array
- */
- protected $templateVars;
- /**
- * Template Options
- *
- * @var array
- */
- protected $templateOptions;
- /**
- * Mail Transport
- *
- * @var \Magento\Framework\Mail\TransportInterface
- */
- protected $transport;
- /**
- * Template Factory
- *
- * @var FactoryInterface
- */
- protected $templateFactory;
- /**
- * Object Manager
- *
- * @var \Magento\Framework\ObjectManagerInterface
- */
- protected $objectManager;
- /**
- * Message
- *
- * @var \Magento\Framework\Mail\Message
- */
- protected $message;
- /**
- * Sender resolver
- *
- * @var \Magento\Framework\Mail\Template\SenderResolverInterface
- */
- protected $_senderResolver;
- /**
- * @var \Magento\Framework\Mail\TransportInterfaceFactory
- */
- protected $mailTransportFactory;
- /**
- * @var \Magento\Framework\Mail\MessageInterfaceFactory
- */
- private $messageFactory;
- /**
- * @param FactoryInterface $templateFactory
- * @param MessageInterface $message
- * @param SenderResolverInterface $senderResolver
- * @param ObjectManagerInterface $objectManager
- * @param TransportInterfaceFactory $mailTransportFactory
- * @param MessageInterfaceFactory $messageFactory
- *
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function __construct(
- FactoryInterface $templateFactory,
- MessageInterface $message,
- SenderResolverInterface $senderResolver,
- ObjectManagerInterface $objectManager,
- TransportInterfaceFactory $mailTransportFactory,
- MessageInterfaceFactory $messageFactory = null
- ) {
- $this->templateFactory = $templateFactory;
- $this->objectManager = $objectManager;
- $this->_senderResolver = $senderResolver;
- $this->mailTransportFactory = $mailTransportFactory;
- $this->messageFactory = $messageFactory ?: $this->objectManager->get(MessageInterfaceFactory::class);
- $this->message = $this->messageFactory->create();
- }
- /**
- * Add cc address
- *
- * @param array|string $address
- * @param string $name
- * @return $this
- */
- public function addCc($address, $name = '')
- {
- $this->message->addCc($address, $name);
- return $this;
- }
- /**
- * Add to address
- *
- * @param array|string $address
- * @param string $name
- * @return $this
- */
- public function addTo($address, $name = '')
- {
- $this->message->addTo($address, $name);
- return $this;
- }
- /**
- * Add bcc address
- *
- * @param array|string $address
- * @return $this
- */
- public function addBcc($address)
- {
- $this->message->addBcc($address);
- return $this;
- }
- /**
- * Set Reply-To Header
- *
- * @param string $email
- * @param string|null $name
- * @return $this
- */
- public function setReplyTo($email, $name = null)
- {
- $this->message->setReplyTo($email, $name);
- return $this;
- }
- /**
- * Set mail from address
- *
- * @deprecated 102.0.1 This function sets the from address but does not provide
- * a way of setting the correct from addresses based on the scope.
- * @see setFromByScope()
- *
- * @param string|array $from
- * @return $this
- * @throws \Magento\Framework\Exception\MailException
- */
- public function setFrom($from)
- {
- return $this->setFromByScope($from, null);
- }
- /**
- * Set mail from address by scopeId
- *
- * @param string|array $from
- * @param string|int $scopeId
- * @return $this
- * @throws \Magento\Framework\Exception\MailException
- * @since 102.0.1
- */
- public function setFromByScope($from, $scopeId = null)
- {
- $result = $this->_senderResolver->resolve($from, $scopeId);
- $this->message->setFromAddress($result['email'], $result['name']);
- return $this;
- }
- /**
- * Set template identifier
- *
- * @param string $templateIdentifier
- * @return $this
- */
- public function setTemplateIdentifier($templateIdentifier)
- {
- $this->templateIdentifier = $templateIdentifier;
- return $this;
- }
- /**
- * Set template model
- *
- * @param string $templateModel
- * @return $this
- */
- public function setTemplateModel($templateModel)
- {
- $this->templateModel = $templateModel;
- return $this;
- }
- /**
- * Set template vars
- *
- * @param array $templateVars
- * @return $this
- */
- public function setTemplateVars($templateVars)
- {
- $this->templateVars = $templateVars;
- return $this;
- }
- /**
- * Set template options
- *
- * @param array $templateOptions
- * @return $this
- */
- public function setTemplateOptions($templateOptions)
- {
- $this->templateOptions = $templateOptions;
- return $this;
- }
- /**
- * Get mail transport
- *
- * @return \Magento\Framework\Mail\TransportInterface
- * @throws LocalizedException
- */
- public function getTransport()
- {
- $this->prepareMessage();
- $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]);
- $this->reset();
- return $mailTransport;
- }
- /**
- * Reset object state
- *
- * @return $this
- */
- protected function reset()
- {
- $this->message = $this->messageFactory->create();
- $this->templateIdentifier = null;
- $this->templateVars = null;
- $this->templateOptions = null;
- return $this;
- }
- /**
- * Get template
- *
- * @return \Magento\Framework\Mail\TemplateInterface
- */
- protected function getTemplate()
- {
- return $this->templateFactory->get($this->templateIdentifier, $this->templateModel)
- ->setVars($this->templateVars)
- ->setOptions($this->templateOptions);
- }
- /**
- * Prepare message.
- *
- * @return $this
- * @throws LocalizedException if template type is unknown
- */
- protected function prepareMessage()
- {
- $template = $this->getTemplate();
- $body = $template->processTemplate();
- switch ($template->getType()) {
- case TemplateTypesInterface::TYPE_TEXT:
- $this->message->setBodyText($body);
- break;
- case TemplateTypesInterface::TYPE_HTML:
- $this->message->setBodyHtml($body);
- break;
- default:
- throw new LocalizedException(
- new Phrase('Unknown template type')
- );
- }
- $this->message->setSubject(html_entity_decode($template->getSubject(), ENT_QUOTES));
- return $this;
- }
- }
|