Recently Magento released 2.4.8 and removed LaminasMime package completely.
We had a module which was sending email with csv attachment now stopped sending the csv as attachment. It just sends the regular email with html content excluding the csv.
Here is the di.xml code
<preference for="MagentoFrameworkMailTemplateTransportBuilder" type="VendorModuleModelMailTemplateAddEmailAttachemnt" />
Then the module function to send email:
$this->inlineTranslation->suspend();
$sender = [
'name' => $this->escaper->escapeHtml('Customer Service'),
'email' => $this->escaper->escapeHtml('[email protected]'),
];
$transport = $this->transportBuilder
->setTemplateIdentifier('hww_mycatalog')
->setTemplateOptions(
[
'area' => MagentoFrameworkAppArea::AREA_FRONTEND,
'store' => $data['website_id'],
]
)
->setTemplateVars([
'name' => $data['customer_name'],
])
->setFrom($sender)
->addTo($data['email'])
->addAttachment(file_get_contents($filePath.$fileName), $fileName, 'text/csv')
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
And finally the AddEmailAttachemnt.php
<?php
declare (strict_types = 1);
namespace VendorModuleModelMailTemplate;
use MagentoFrameworkAppTemplateTypesInterface;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkMailAddressConverter;
use MagentoFrameworkMailEmailMessageInterfaceFactory;
use MagentoFrameworkMailMessageInterface;
use MagentoFrameworkMailMessageInterfaceFactory;
use MagentoFrameworkMailMimeInterface;
use MagentoFrameworkMailMimeMessageInterfaceFactory;
use MagentoFrameworkMailMimePartInterfaceFactory;
use MagentoFrameworkMailTemplateFactoryInterface;
use MagentoFrameworkMailTemplateSenderResolverInterface;
use MagentoFrameworkMailTransportInterfaceFactory;
use MagentoFrameworkObjectManagerInterface;
use MagentoFrameworkPhrase;
use LaminasMimeMime;
use LaminasMimePartFactory;
class AddEmailAttachemnt extends MagentoFrameworkMailTemplateTransportBuilder
{
protected $templateIdentifier;
protected $templateModel;
protected $templateVars;
protected $templateOptions;
protected $transport;
protected $templateFactory;
protected $objectManager;
protected $message;
protected $_senderResolver;
protected $mailTransportFactory;
private $messageData = [];
private $emailMessageInterfaceFactory;
private $mimeMessageInterfaceFactory;
private $mimePartInterfaceFactory;
private $addressConverter;
protected $attachments = [];
protected $partFactory;
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
) {
$this->templateFactory = $templateFactory;
$this->objectManager = $objectManager;
$this->_senderResolver = $senderResolver;
$this->mailTransportFactory = $mailTransportFactory;
$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);
$this->partFactory = $objectManager->get(PartFactory::class);
parent::__construct(
$templateFactory,
$message,
$senderResolver,
$objectManager,
$mailTransportFactory,
$messageFactory,
$emailMessageInterfaceFactory,
$mimeMessageInterfaceFactory,
$mimePartInterfaceFactory,
$addressConverter
);
}
public function addCc($address, $name = '')
{
$this->addAddressByType('cc', $address, $name);
return $this;
}
public function addTo($address, $name = '')
{
$this->addAddressByType('to', $address, $name);
return $this;
}
public function addBcc($address)
{
$this->addAddressByType('bcc', $address);
return $this;
}
public function setReplyTo($email, $name = null)
{
$this->addAddressByType('replyTo', $email, $name);
return $this;
}
public function setFrom($from)
{
return $this->setFromByScope($from);
}
public function setFromByScope($from, $scopeId = null)
{
$result = $this->_senderResolver->resolve($from, $scopeId);
$this->addAddressByType('from', $result['email'], $result['name']);
return $this;
}
public function setTemplateIdentifier($templateIdentifier)
{
$this->templateIdentifier = $templateIdentifier;
return $this;
}
public function setTemplateModel($templateModel)
{
$this->templateModel = $templateModel;
return $this;
}
public function setTemplateVars($templateVars)
{
$this->templateVars = $templateVars;
return $this;
}
public function setTemplateOptions($templateOptions)
{
$this->templateOptions = $templateOptions;
return $this;
}
public function getTransport()
{
try {
$this->prepareMessage();
$mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]);
} finally {
$this->reset();
}
return $mailTransport;
}
protected function reset()
{
$this->messageData = [];
$this->templateIdentifier = null;
$this->templateVars = null;
$this->templateOptions = null;
return $this;
}
protected function getTemplate()
{
return $this->templateFactory->get($this->templateIdentifier, $this->templateModel)
->setVars($this->templateVars)
->setOptions($this->templateOptions);
}
protected function prepareMessage()
{
$template = $this->getTemplate();
$content = $template->processTemplate();
switch ($template->getType()) {
case TemplateTypesInterface::TYPE_TEXT:
$part['type'] = MimeInterface::TYPE_TEXT;
break;
case TemplateTypesInterface::TYPE_HTML:
$part['type'] = MimeInterface::TYPE_HTML;
break;
default:
throw new LocalizedException(
new Phrase('Unknown template type')
);
}
$mimePart = $this->mimePartInterfaceFactory->create(['content' => $content]);
$parts = count($this->attachments) ? array_merge([$mimePart], $this->attachments) : [$mimePart];
$this->messageData['body'] = $this->mimeMessageInterfaceFactory->create(
['parts' => $parts]
);
$this->messageData['subject'] = html_entity_decode(
(string) $template->getSubject(),
ENT_QUOTES
);
$this->message = $this->emailMessageInterfaceFactory->create($this->messageData);
return $this;
}
private function addAddressByType($addressType, $email, $name = null): void
{
if (is_string($email)) {
$this->messageData[$addressType][] = $this->addressConverter->convert($email, $name);
return;
}
$convertedAddressArray = $this->addressConverter->convertMany($email);
if (isset($this->messageData[$addressType])) {
$this->messageData[$addressType] = array_merge(
$this->messageData[$addressType],
$convertedAddressArray
);
}
}
public function addAttachment($content, $fileName, $fileType)
{
$attachmentPart = $this->partFactory->create();
$attachmentPart->setContent($content)
->setType($fileType)
->setFileName($fileName)
->setDisposition(Mime::DISPOSITION_ATTACHMENT)
->setEncoding(Mime::ENCODING_BASE64);
$this->attachments[] = $attachmentPart;
return $this;
}
}