How to Add Attachments with Email in Magento 2.3.x
The Magento 2.3.x versions use the Zend Framework 2. The implementation of various functionalities is different from the previous versions as it refuses to apply Zend Framework 1 (ZF1).
One such function is to send Emails with attachments. With the function createAttachment() in the previous Magento 2 versions, it was easy to add attachments in Emails.
However, if you are using the upgraded versions (I’m sure you’re enough concerned with the security and thus using the latest Magento 2.3.2), use the below code to add attachments with Email in Magento 2.3.x.
You may refer the below manuals for more knowledge about ZF2 components:
Method to Add Attachments with Email in Magento 2.3.x:
- Add following code in di.xmlNote:If you want to attach the file in frontend – vendor\module\etc\frontend\di.xml
If you want to attach the file in backend– vendor\module\etc\adminhtml\di.xml
If you want to attach the file in both frontend and backend – vendor\module\etc\di.xml
123456789<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"><type name="Vendor\Module\Mail\Template\TransportBuilder"><arguments><argument name="message" xsi:type="object" >vendor\module\Mail\Message</argument><argument name="messageFactory" xsi:type="object" >vendor\module\Mail\MessageFactory</argument></arguments></type></config> - Create Message.php file at Vendor\Module\Mail\Message.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132<?phpnamespace Vendor\Module\Mail;use Zend\Mime\Mime;use Zend\Mime\PartFactory;use Zend\Mail\MessageFactory as MailMessageFactory;use Zend\Mime\MessageFactory as MimeMessageFactory;use Magento\Framework\Mail\MailMessageInterface;class Message implements MailMessageInterface{protected $partFactory;protected $mimeMessageFactory;private $zendMessage;protected $parts = [];public function __construct(PartFactory $partFactory,MimeMessageFactory $mimeMessageFactory,$charset = 'utf-8'){$this->partFactory = $partFactory;$this->mimeMessageFactory = $mimeMessageFactory;$this->zendMessage = MailMessageFactory::getInstance();$this->zendMessage->setEncoding($charset);}public function setBodyText($content){$textPart = $this->partFactory->create();$textPart->setContent($content)->setType(Mime::TYPE_TEXT)->setCharset($this->zendMessage->getEncoding());$this->parts[] = $textPart;return $this;}public function setBodyHtml($content){$htmlPart = $this->partFactory->create();$htmlPart->setContent($content)->setType(Mime::TYPE_HTML)->setCharset($this->zendMessage->getEncoding());$this->parts[] = $htmlPart;return $this;}public function setBodyAttachment($content, $fileName){$fileType = Mime::TYPE_OCTETSTREAM;$attachmentPart = $this->partFactory->create();$attachmentPart->setContent($content)->setType($fileType)->setFileName($fileName)->setDisposition(Mime::DISPOSITION_ATTACHMENT)->setEncoding(Mime::ENCODING_BASE64);$this->parts[] = $attachmentPart;return $this;}public function setPartsToBody(){$mimeMessage = $this->mimeMessageFactory->create();$mimeMessage->setParts($this->parts);$this->zendMessage->setBody($mimeMessage);return $this;}public function setBody($body){return $this;}public function setSubject($subject){$this->zendMessage->setSubject($subject);return $this;}public function getSubject(){return $this->zendMessage->getSubject();}public function getBody(){return $this->zendMessage->getBody();}public function setFrom($fromAddress){$this->zendMessage->setFrom($fromAddress);return $this;}public function addTo($toAddress){$this->zendMessage->addTo($toAddress);return $this;}public function addCc($ccAddress){$this->zendMessage->addCc($ccAddress);return $this;}public function addBcc($bccAddress){$this->zendMessage->addBcc($bccAddress);return $this;}public function setReplyTo($replyToAddress){$this->zendMessage->setReplyTo($replyToAddress);return $this;}public function getRawMessage(){return $this->zendMessage->toString();}public function setMessageType($type){return $this;}} - Create MessageFactory.php file at Vendor\Module\Mail\MessageFactory.php
12345678910111213141516171819202122232425262728293031323334<?phpnamespace Vendor\Module\Mail;class MessageFactory extends \Magento\Framework\Mail\MessageInterfaceFactory{/*** @var \Magento\Framework\ObjectManagerInterface*/protected $_objectManager;/*** Factory constructor** @param \Magento\Framework\ObjectManagerInterface $objectManager* @param string $instanceName*/public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager, $instanceName = '\\Extait\\Attachment\\Mail\\Message'){$this->_objectManager = $objectManager;$this->_instanceName = $instanceName;}/*** Create class instance with specified parameters** @param array $data* @return \Magento\Framework\Mail\MessageInterface*/public function create(array $data = []){return $this->_objectManager->create($this->_instanceName, $data);}} - Create TransportBuilder.php file at Vendor\Module\Model\Mail\Template\TransportBuilder.php
1234567891011121314151617181920212223242526272829<?phpnamespace Vendor\Module\Model\Mail\Template;class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder{public function addAttachment($fileString,$filename){$arrContextOptions = ["ssl" => ["verify_peer" => false,"verify_peer_name" => false,]];/* if $fileString is url of file */$this->message->setBodyAttachment(file_get_contents($fileString, false, stream_context_create($arrContextOptions)), $filename);/* if $fileString is string data */$this->message->setBodyAttachment($fileString, $filename);return $this;}protected function prepareMessage(){parent::prepareMessage();$this->message->setPartsToBody();return $this;}} - Use TransportBuilder object in the file where you want to add an attachment in the Email.
For example Vendor/Module/Helper/Data.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465<?phpnamespace Vendor\Extension\Helper;use Magento\Customer\Model\Session;class Data extends \Magento\Framework\App\Helper\AbstractHelper{const XML_PATH_EMAIL_DEMO = 'emaildemo/email/email_demo_template';protected $inlineTranslation;protected $transportBuilder;protected $template;protected $storeManager;public function __construct(\Magento\Framework\App\Helper\Context $context,\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,\Vendor\Module\Model\Mail\Template\TransportBuilder $transportBuilder,\Magento\Store\Model\StoreManagerInterface $storeManager){$this->inlineTranslation = $inlineTranslation;$this->transportBuilder = $transportBuilder;$this->storeManager = $storeManager;parent::__construct($context);}public function generateTemplate(){// path for attachment File$attachmentFile = 'file_path/email.pdf';$fileName = 'email.pdf';$emailTemplateVariables['message'] = 'This is a test message by meetanshi.';//load your email tempate$this->_template = $this->scopeConfig->getValue(self::XML_PATH_EMAIL_DEMO,\Magento\Store\Model\ScopeInterface::SCOPE_STORE,$this->_storeManager->getStore()->getStoreId());$this->_inlineTranslation->suspend();$this->_transportBuilder->setTemplateIdentifier($this->_template)->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND,'store' => $this->_storeManager->getStore()->getId(),])->setTemplateVars($emailTemplateVariables)->setFrom(['name' => 'Meetanshi',])->addAttachment($attachmentFile,$fileName); //Attachment goes here.try {$transport = $this->_transportBuilder->getTransport();$transport->sendMessage();$this->_inlineTranslation->resume();} catch (\Exception $e) {echo $e->getMessage(); die;}}
Method to Add Attachments with Email in Magento 2.3.3:
- Create di.xml file at app/code/vendor/module/etc/
1<preference for="\Magento\Framework\Mail\Template\TransportBuilder" type="Vendor\Module\Model\Mail\Template\TransportBuilder" /> - Create TransportBuilder.php at app/code/vendor/module/model/mail/template/
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459<?phpdeclare(strict_types=1);namespace Vendor\Module\Model\Mail\Template;use Magento\Framework\App\TemplateTypesInterface;use Magento\Framework\Exception\LocalizedException;use Magento\Framework\Exception\MailException;use Magento\Framework\Mail\EmailMessageInterface;use Magento\Framework\Mail\EmailMessageInterfaceFactory;use Magento\Framework\Mail\AddressConverter;use Magento\Framework\Mail\Exception\InvalidArgumentException;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\TemplateInterface;use Magento\Framework\Mail\TransportInterface;use Magento\Framework\Mail\TransportInterfaceFactory;use Magento\Framework\ObjectManagerInterface;use Magento\Framework\Phrase;use Zend\Mime\Mime;use Zend\Mime\PartFactory;/*** TransportBuilder** @api* @SuppressWarnings(PHPMD.CouplingBetweenObjects)* @since 100.0.2*/class TransportBuilder extends \Magento\Framework\Mail\Template\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 TransportInterface*/protected $transport;/*** Template Factory** @var FactoryInterface*/protected $templateFactory;/*** Object Manager** @var ObjectManagerInterface*/protected $objectManager;/*** Message** @var EmailMessageInterface*/protected $message;/*** Sender resolver** @var SenderResolverInterface*/protected $_senderResolver;/*** @var TransportInterfaceFactory*/protected $mailTransportFactory;/*** 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;protected $attachments = [];protected $partFactory;/*** 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) {$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);}/*** Add cc address** @param array|string $address* @param string $name** @return $this*/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 InvalidArgumentException*/public function addTo($address, $name = ''){$this->addAddressByType('to', $address, $name);return $this;}/*** Add bcc address** @param array|string $address** @return $this* @throws InvalidArgumentException*/public function addBcc($address){$this->addAddressByType('bcc', $address);return $this;}/*** Set Reply-To Header** @param string $email* @param string|null $name** @return $this* @throws InvalidArgumentException*/public function setReplyTo($email, $name = null){$this->addAddressByType('replyTo', $email, $name);return $this;}/*** Set mail from address** @param string|array $from** @return $this* @throws InvalidArgumentException* @see setFromByScope()** @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.*/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 InvalidArgumentException* @throws MailException* @since 102.0.1*/public function setFromByScope($from, $scopeId = null){$result = $this->_senderResolver->resolve($from, $scopeId);$this->addAddressByType('from', $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 TransportInterface* @throws LocalizedException*/public function getTransport(){try {$this->prepareMessage();$mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]);} finally {$this->reset();}return $mailTransport;}/*** Reset object state** @return $this*/protected function reset(){$this->messageData = [];$this->templateIdentifier = null;$this->templateVars = null;$this->templateOptions = null;return $this;}/*** Get template** @return 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();$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;}/*** Handles possible incoming types of email (string or array)** @param string $addressType* @param string|array $email* @param string|null $name** @return void* @throws InvalidArgumentException*/private function addAddressByType(string $addressType, $email, ?string $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);}}/*** @param string|null $content* @param string|null $fileName* @param string|null $fileType* @return TransportBuilder*/public function addAttachment(?string $content, ?string $fileName, ?string $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;}}
Add attachment in email like :
1 |
$this->transportBuilder->addAttachment($content, $fileName, 'application/pdf'); |
That’s all you need to do for implementing Emails with attachments in Magento 2.3.x
Please use the Comments section below to mention any doubts on the topic. I’d be glad to help you out.
You may share the above solution with the Magento community via social media.
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
53 Comments
Hello,
Can you please provide the solution to add attachments in mail in Magento Version 2.4.x ?
Hello Parthavi,
The above code is not for Magento 2.4.x
I will post the solution in the future.
Do subscribe to Meetanshi’s blog posts to get notified for the same.
Thank You
Hello Sanjay,
Is it work form mangento ver. 2.4.3?
Hello Bhupendra,
It works only for Magento 2.3.x.
Thank You
Hi Sanjay
I tried your code with 2.4.1. it is sending the attachment (pdf) but attachment when I download it says ” File type plain text document (text/plain) is not supported” Even I used the ->addAttachment($attachmentFile,$fileName, ‘application/pdf’); //Attachment goes here.
//$this->_transportBuilder->addAttachment($attachmentFile,$fileName, ‘application/pdf’);
and correct path of the pdf file.
Do you have any Idea what I am missing?
Hello Pawan,
The \zend\mime has been removed from the Magento version above the 2.4.1.
We can use \Laminas\Mime instead of that.
Thank You.
Hi
I used but not working, do you have blog for 2.4.1?
if, can you share with me?
Thanks
Hello Pawan,
The above code is not for Magento 2.4.x
I will post the solution in the future.
Do subscribe to Meetanshi’s blog posts to get notified for the same.
Thank You.
Hello Sanjay,
It’s not working with Magento 2.4. Can you please update the code?
Thanks
Debendra
Hello,
I will post the solution in the future.
Do subscribe to Meetanshi’s blog posts to get notified for the same.
Thank You.
Hello Sanjay!
I’m using Magento-2.3.4.
Will it work for CSV attachment as well?
If I want to attach a CSV file instead of a pdf then what would be the file_type for the function addAttachment($content, $fileName, ??).
Thanks!
Hello,
Use File Type ‘application/octet-stream’ as shown below:
$this->transportBuilder->addAttachment($content, $fileName, ‘application/octet-stream’);
Thank You.
Hello sanjay Bhai
i am using second solution i am able to send one pdf in mail but i am not able to send multiple pdf in same mail.
OODE
$transport = $this->_transportBuilder->setTemplateIdentifier(‘id’)
->setTemplateOptions($templateOptions)
->setTemplateVars($templateVars)
->setFrom($from)
->addTo($to)
->addAttachment($this->reader->fileGetContents($pdf1), ‘pdf name’, ‘application/pdf’)
->addAttachment($this->reader->fileGetContents($pdf2), ‘pdf name’, ‘application/pdf’)
->getTransport();
Hello,
Multiple attachment is working in the above code.
Please mention the exact error that you are facing
Thanks.
Hello Sanjay Sir,
I am using Magneto2.3.1, which code will work for me to send attachments in email?
Hello Hussain,
Please use the method listed below the heading “Method to Add Attachments with Email in Magento 2.3.x” for your requirements.
Thank you.
Hi Team,
I’m unable to send xml file as an attachment in magento 2.3.6 p1 CE version. I was able to send it in 2.2.2 version.
Could you please suggest me an workaround
Hello,
In Magento 2.3.6 version use \Laminas\Mime instead of Zend\Mime
Thank you.
Hello,
whare we can download this module ??
Hello Niranjan,
This is not a code of the complete module. You need to create a custom mode for that and implement the given code.
Thank you.
working for magento 2.4.1?
Hello,
The above solution works for Magento 2.3.x.
I’ll update the post for Magento 2.4.1 and inform you.
Thanks.
I too would like to know more details of how to use this for a contact form, it would be a lifesaver. There seems to be little documentation on this and your article seems the most comprehensive so far. If only it also adapted for a contact us form it would be perfect
Hello Jean,
Please refer here – https://github.com/magento/magento2-samples/tree/2.1/sample-module-payment-gateway
Thanks.
Hello i am facing problem in this function
protected function prepareMessage()
{
parent::prepareMessage();
$this->message->setPartsToBody();
return $this;
}
here this->message is changed to use Magento\Framework\Mail\EmailMessageInterfaceFactory; this class
so i am getting error
Call to undefined method Magento\\Framework\\Mail\\EmailMessage\\Interceptor::setPartsToBody()
i’m using magento 2.3.4
any idea to fix this??
Hello Prabhakaran,
There are two methods in the above post.
You need to follow the method to “Add Attachments with Email in Magento 2.3.3”
Thank you.
Hi Sanjay,
is there source available for Send Mail with attachment
Magento 2.3.5-p2
use Zend\Mime\Mime;
use Zend\Mime\PartFactory;
this two main thing are not in Magento 2.3.5-p2
Hello Prabhakaran,
No, its not available for Magento 2.3.5-p2
I’ll update the code for this version and inform you.
Thanks.
Hi sanjay,
I’m to send pdf attachment as order is placed placed from backed or fronted. I have follow this blog but mail is received but no pdf get
Hello Nitish,
Which Magento version are you using?
Are you getting any error message in var/log?
Please try to place log and debug because it seems as if there’s some mistake in the data passing of path or pdf.
Thank you.
Hi Sanjay,
In this call : $this->transportBuilder->addAttachment($content, $fileName, ‘application/pdf’);
what is $content?
Hello,
$content is content(string) value of the attachment file.
Pass the file content that you get from file_get_contents() function
Thanks.
Thanks it works.
But file content comes with only 1kb. content not load propely in email.
Hello Sanjay Sir,
File attached done properly. But the content does not load properly. File size in email 1kb where actual size is 80kb.
Below is my code
->addAttachment($attachment, $fileName, ‘application/pdf’);
Where $attachment variable contail file path.
Can you please look into my issue?
Hello Ravi,
What is your Magento 2 version?
The file path may not be given properly as the attachment added is blank.
Thank you.
I am getting below error after implementing this code:
Class Zend\\Mime\\PartFactory does not exist
Any idea?
Hello Viral,
Which Magento 2 version are you using?
Thank you for your quick reply. I am using Magento 2.3.3. Anyways I found a workaround that I am posting here so it might be helpful to anyone facing the same problem. I had to change below line (TransportBuilder.php):
$this->partFactory = $objectManager->get(PartFactory::class);
to
$this->partFactory = $partFactory ?: $objectManager->get(PartFactory::class);
Thank you for sharing the post. Working fine now.
Hey Viral, thanks a lot for sharing this.
This is not working for Magento 2.3.5-p1
We double checked the code with Magento 2.3.5-p1 and it works well with it.
Hi,
First I thought the 2.3.x solution was also meant for 2.3.4, but that didn’t work, Using the 2.3.3 fix was the solution!
Thanks.
Hey, Thank you for your appreciation.
IS IT WORKING FOR 2.3.4
Hello,
Yes, the above solution works for Magento 2.3.4
Thank you.
Its working.
you save my life.
great post.
Hey Nick!
Happy to help.
Thank you so much for the appreciation 🙂
This is working in Magento 2.3.3. Thanks
Happy to help.
Thanks for the appreciation 🙂
Hi thank you for share this with us, can you help me, we can use this for contact us page? If yes can you give me an example please?
Hello,
You can override the data passed in the contact form and use this code.
Thanks.
This solution not working for Magento 2.3.3
Hi!
I’ve updated the blog with the solution for Magento 2.3.3.
Please check and feel free to approach for any further doubts on the topic.
Thanks.