Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
Merge pull request #4600 from magento-techdivision/mail_interface_cle…
Browse files Browse the repository at this point in the history
…anup

MC-15295: Mail interface cleanup
  • Loading branch information
Joan He committed Aug 4, 2019
2 parents 1d9e07b + f3a7d04 commit 41daebf
Show file tree
Hide file tree
Showing 23 changed files with 1,888 additions and 208 deletions.
7 changes: 5 additions & 2 deletions app/code/Magento/Email/Model/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Email\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Mail\EmailMessageInterface;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\TransportInterface;
use Magento\Framework\Phrase;
Expand Down Expand Up @@ -59,12 +62,12 @@ class Transport implements TransportInterface
private $message;

/**
* @param MessageInterface $message Email message object
* @param EmailMessageInterface $message Email message object
* @param ScopeConfigInterface $scopeConfig Core store config
* @param null|string|array|\Traversable $parameters Config options for sendmail parameters
*/
public function __construct(
MessageInterface $message,
EmailMessageInterface $message,
ScopeConfigInterface $scopeConfig,
$parameters = null
) {
Expand Down
249 changes: 244 additions & 5 deletions app/code/Magento/Newsletter/Model/Queue/TransportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,29 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Newsletter\Model\Queue;

use Magento\Email\Model\AbstractTemplate;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Mail\EmailMessageInterfaceFactory;
use Magento\Framework\Mail\AddressConverter;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\MessageInterfaceFactory;
use Magento\Framework\Mail\MimeMessageInterfaceFactory;
use Magento\Framework\Mail\MimePartInterfaceFactory;
use Magento\Framework\Mail\Template\FactoryInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\Mail\TemplateInterface;
use Magento\Framework\Mail\TransportInterfaceFactory;
use Magento\Framework\ObjectManagerInterface;

/**
* Class TransportBuilder
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
/**
Expand All @@ -16,6 +35,194 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
*/
protected $templateData = [];

/**
* Param that used for storing all message data until it will be used
*
* @var array
*/
private $messageData = [];

/**
* @var EmailMessageInterfaceFactory
*/
private $emailMessageInterfaceFactory;

/**
* @var MimeMessageInterfaceFactory
*/
private $mimeMessageInterfaceFactory;

/**
* @var MimePartInterfaceFactory
*/
private $mimePartInterfaceFactory;

/**
* @var AddressConverter|null
*/
private $addressConverter;

/**
* TransportBuilder constructor
*
* @param FactoryInterface $templateFactory
* @param MessageInterface $message
* @param SenderResolverInterface $senderResolver
* @param ObjectManagerInterface $objectManager
* @param TransportInterfaceFactory $mailTransportFactory
* @param MessageInterfaceFactory|null $messageFactory
* @param EmailMessageInterfaceFactory|null $emailMessageInterfaceFactory
* @param MimeMessageInterfaceFactory|null $mimeMessageInterfaceFactory
* @param MimePartInterfaceFactory|null $mimePartInterfaceFactory
* @param AddressConverter|null $addressConverter
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
FactoryInterface $templateFactory,
MessageInterface $message,
SenderResolverInterface $senderResolver,
ObjectManagerInterface $objectManager,
TransportInterfaceFactory $mailTransportFactory,
MessageInterfaceFactory $messageFactory = null,
EmailMessageInterfaceFactory $emailMessageInterfaceFactory = null,
MimeMessageInterfaceFactory $mimeMessageInterfaceFactory = null,
MimePartInterfaceFactory $mimePartInterfaceFactory = null,
AddressConverter $addressConverter = null
) {
parent::__construct(
$templateFactory,
$message,
$senderResolver,
$objectManager,
$mailTransportFactory,
$messageFactory,
$emailMessageInterfaceFactory,
$mimeMessageInterfaceFactory,
$mimePartInterfaceFactory,
$addressConverter
);
$this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $this->objectManager
->get(EmailMessageInterfaceFactory::class);
$this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $this->objectManager
->get(MimeMessageInterfaceFactory::class);
$this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $this->objectManager
->get(MimePartInterfaceFactory::class);
$this->addressConverter = $addressConverter ?: $this->objectManager
->get(AddressConverter::class);
}

/**
* Add cc address
*
* @param array|string $address
* @param string $name
*
* @return \Magento\Framework\Mail\Template\TransportBuilder
* @throws MailException
*/
public function addCc($address, $name = '')
{
$this->addAddressByType('cc', $address, $name);

return $this;
}

/**
* Add to address
*
* @param array|string $address
* @param string $name
*
* @return $this
* @throws MailException
*/
public function addTo($address, $name = '')
{
$this->addAddressByType('to', $address, $name);

return $this;
}

/**
* Add bcc address
*
* @param array|string $address
*
* @return $this
* @throws MailException
*/
public function addBcc($address)
{
$this->addAddressByType('bcc', $address);

return $this;
}

/**
* Set Reply-To Header
*
* @param string $email
* @param string|null $name
*
* @return $this
* @throws MailException
*/
public function setReplyTo($email, $name = null)
{

$this->addAddressByType('replyTo', $email, $name);

return $this;
}

/**
* Set mail from address
*
* @param string|array $from
*
* @return $this
* @throws MailException
* @see setFromByScope()
*
* @deprecated This function sets the from address but does not provide
* a way of setting the correct from addresses based on the scope.
*/
public function setFrom($from)
{
return $this->setFromByScope($from);
}

/**
* Set mail from address by scopeId
*
* @param string|array $from
* @param string|int $scopeId
*
* @return $this
* @throws MailException
*/
public function setFromByScope($from, $scopeId = null)
{
$result = $this->_senderResolver->resolve($from, $scopeId);
$this->addAddressByType('from', $result['email'], $result['name']);

return $this;
}

/**
* @inheritDoc
*/
protected function reset()
{
$this->messageData = [];
$this->templateIdentifier = null;
$this->templateVars = null;
$this->templateOptions = null;

return $this;
}

/**
* Set template data
*
Expand All @@ -25,11 +232,15 @@ class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
public function setTemplateData($data)
{
$this->templateData = $data;

return $this;
}

/**
* Sets up template filter
*
* @param AbstractTemplate $template
*
* @return void
*/
protected function setTemplateFilter(AbstractTemplate $template)
Expand All @@ -44,16 +255,44 @@ protected function setTemplateFilter(AbstractTemplate $template)
*/
protected function prepareMessage()
{
/** @var AbstractTemplate $template */
/** @var AbstractTemplate|TemplateInterface $template */
$template = $this->getTemplate()->setData($this->templateData);
$this->setTemplateFilter($template);
$content = $template->getProcessedTemplate($this->templateVars);
$this->messageData['subject'] = $template->getSubject();

$this->message->setBodyHtml(
$template->getProcessedTemplate($this->templateVars)
)->setSubject(
$template->getSubject()
$mimePart = $this->mimePartInterfaceFactory->create(
['content' => $content]
);
$this->messageData['body'] = $this->mimeMessageInterfaceFactory->create(
['parts' => [$mimePart]]
);

$this->message = $this->emailMessageInterfaceFactory->create($this->messageData);

return $this;
}

/**
* Handles possible incoming types of email (string or array)
*
* @param string $addressType
* @param string|array $email
* @param string|null $name
*
* @return void
* @throws MailException
*/
private function addAddressByType(string $addressType, $email, ?string $name = null): void
{
if (is_array($email)) {
$this->messageData[$addressType] = array_merge(
$this->messageData[$addressType],
$this->addressConverter->convertMany($email)
);

return;
}
$this->messageData[$addressType][] = $this->addressConverter->convert($email, $name);
}
}
Loading

0 comments on commit 41daebf

Please sign in to comment.