init
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Sales\Model\Order\Template;
|
||||
|
||||
use Magento\Framework\App\TemplateTypesInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Mail\AddressConverter;
|
||||
use Magento\Framework\Mail\EmailMessageInterfaceFactory;
|
||||
use Magento\Framework\Mail\MessageInterface;
|
||||
use Magento\Framework\Mail\MessageInterfaceFactory;
|
||||
use Magento\Framework\Mail\MimeInterface;
|
||||
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\TransportInterfaceFactory;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Magento\Framework\Phrase;
|
||||
|
||||
class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
|
||||
{
|
||||
/**
|
||||
* Param that used for storing all message data until it will be used
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected array $messageData = [];
|
||||
|
||||
/**
|
||||
* @var EmailMessageInterfaceFactory
|
||||
*/
|
||||
protected EmailMessageInterfaceFactory $emailMessageInterfaceFactory;
|
||||
|
||||
/**
|
||||
* @var MimeMessageInterfaceFactory
|
||||
*/
|
||||
protected MimeMessageInterfaceFactory $mimeMessageInterfaceFactory;
|
||||
|
||||
protected ?\Magento\Framework\Mail\MimePartInterface $attachmentMimePart = null;
|
||||
|
||||
/**
|
||||
* @var MimePartInterfaceFactory
|
||||
*/
|
||||
protected MimePartInterfaceFactory $mimePartInterfaceFactory;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $messageData
|
||||
*/
|
||||
public function setMessageData(string $key, array $emails): static
|
||||
{
|
||||
$data = [];
|
||||
foreach ($emails as $email) {
|
||||
if (is_string($email)) {
|
||||
$data[] = new \Magento\Framework\Mail\Address($email, $email);
|
||||
}
|
||||
}
|
||||
if (!empty($data))
|
||||
$this->messageData[$key] = $data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare message.
|
||||
*
|
||||
* @return $this
|
||||
* @throws LocalizedException if template type is unknown
|
||||
*/
|
||||
protected function prepareMessage()
|
||||
{
|
||||
$template = $this->getTemplate();
|
||||
$content = $template->processTemplate();
|
||||
|
||||
switch ($template->getType()) {
|
||||
case TemplateTypesInterface::TYPE_TEXT:
|
||||
$partType = MimeInterface::TYPE_TEXT;
|
||||
break;
|
||||
|
||||
case TemplateTypesInterface::TYPE_HTML:
|
||||
$partType = MimeInterface::TYPE_HTML;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new LocalizedException(
|
||||
new Phrase('Unknown template type')
|
||||
);
|
||||
}
|
||||
|
||||
/** @var \Magento\Framework\Mail\MimePartInterface $mimePart */
|
||||
$mimePart = $this->mimePartInterfaceFactory->create(
|
||||
[
|
||||
'content' => $content,
|
||||
'type' => $partType
|
||||
]
|
||||
);
|
||||
|
||||
$this->messageData['sender'] = $mimePart->getCharset();
|
||||
$this->messageData['encoding'] = $mimePart->getCharset();
|
||||
$parts = [$mimePart];
|
||||
if ($this->attachmentMimePart!=null) {
|
||||
$parts[] = $this->attachmentMimePart;
|
||||
}
|
||||
$this->messageData['body'] = $this->mimeMessageInterfaceFactory->create(
|
||||
['parts' => $parts]
|
||||
);
|
||||
|
||||
$this->messageData['subject'] = html_entity_decode(
|
||||
(string)$template->getSubject(),
|
||||
ENT_QUOTES
|
||||
);
|
||||
$this->messageData['sender'] = new \Magento\Framework\Mail\Address('no-reply@stemcellmart.com.cn', 'STEMCELL在线商城');
|
||||
$this->messageData['form'] = $this->messageData['sender'];
|
||||
|
||||
|
||||
$this->message = $this->emailMessageInterfaceFactory->create($this->messageData);
|
||||
$this->message->setFromAddress('no-reply@stemcellmart.com.cn', 'STEMCELL经销商下单系统');
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function setAttachmentMimePart(array $attachment): static
|
||||
{
|
||||
$this->attachmentMimePart = $this->mimePartInterfaceFactory->create($attachment);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user