Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TransportBuilder.php
Go to the documentation of this file.
1 <?php
10 
14 use Magento\Framework\Mail\MessageInterfaceFactory;
18 
25 {
32 
38  protected $templateModel;
39 
45  protected $templateVars;
46 
52  protected $templateOptions;
53 
59  protected $transport;
60 
66  protected $templateFactory;
67 
73  protected $objectManager;
74 
80  protected $message;
81 
87  protected $_senderResolver;
88 
93 
97  private $messageFactory;
98 
109  public function __construct(
112  SenderResolverInterface $senderResolver,
115  MessageInterfaceFactory $messageFactory = null
116  ) {
117  $this->templateFactory = $templateFactory;
118  $this->objectManager = $objectManager;
119  $this->_senderResolver = $senderResolver;
120  $this->mailTransportFactory = $mailTransportFactory;
121  $this->messageFactory = $messageFactory ?: $this->objectManager->get(MessageInterfaceFactory::class);
122  $this->message = $this->messageFactory->create();
123  }
124 
132  public function addCc($address, $name = '')
133  {
134  $this->message->addCc($address, $name);
135  return $this;
136  }
137 
145  public function addTo($address, $name = '')
146  {
147  $this->message->addTo($address, $name);
148  return $this;
149  }
150 
157  public function addBcc($address)
158  {
159  $this->message->addBcc($address);
160  return $this;
161  }
162 
170  public function setReplyTo($email, $name = null)
171  {
172  $this->message->setReplyTo($email, $name);
173  return $this;
174  }
175 
182  public function setFrom($from)
183  {
184  $result = $this->_senderResolver->resolve($from);
185  $this->message->setFrom($result['email'], $result['name']);
186  return $this;
187  }
188 
196  {
197  $this->templateIdentifier = $templateIdentifier;
198  return $this;
199  }
200 
208  {
209  $this->templateModel = $templateModel;
210  return $this;
211  }
212 
220  {
221  $this->templateVars = $templateVars;
222  return $this;
223  }
224 
232  {
233  $this->templateOptions = $templateOptions;
234  return $this;
235  }
236 
242  public function getTransport()
243  {
244  $this->prepareMessage();
245  $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]);
246  $this->reset();
247 
248  return $mailTransport;
249  }
250 
256  protected function reset()
257  {
258  $this->message = $this->messageFactory->create();
259  $this->templateIdentifier = null;
260  $this->templateVars = null;
261  $this->templateOptions = null;
262  return $this;
263  }
264 
270  protected function getTemplate()
271  {
272  return $this->templateFactory->get($this->templateIdentifier, $this->templateModel)
273  ->setVars($this->templateVars)
274  ->setOptions($this->templateOptions);
275  }
276 
283  protected function prepareMessage()
284  {
285  $template = $this->getTemplate();
286  $body = $template->processTemplate();
287  switch ($template->getType()) {
289  $this->message->setBodyText($body);
290  break;
291 
293  $this->message->setBodyHtml($body);
294  break;
295 
296  default:
297  throw new LocalizedException(
298  new Phrase('Unknown template type')
299  );
300  }
301  $this->message->setSubject(html_entity_decode($template->getSubject(), ENT_QUOTES));
302  return $this;
303  }
304 }
$email
Definition: details.phtml:13
$address
Definition: customer.php:38
$template
Definition: export.php:12
__construct(FactoryInterface $templateFactory, MessageInterface $message, SenderResolverInterface $senderResolver, ObjectManagerInterface $objectManager, TransportInterfaceFactory $mailTransportFactory, MessageInterfaceFactory $messageFactory=null)
if(!isset($_GET['name'])) $name
Definition: log.php:14