{"id":601,"date":"2019-09-16T23:58:00","date_gmt":"2019-09-16T23:58:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/09\/16\/add-attachments-with-email-in-magento-2-3-x\/"},"modified":"2025-05-22T15:59:15","modified_gmt":"2025-05-22T10:29:15","slug":"add-attachments-with-email-in-magento-2-3-x","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-attachments-with-email-in-magento-2-3-x\/","title":{"rendered":"How to Add Attachments with Email in Magento 2.3.x"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, if you are using the upgraded versions, use the below code to add attachments with Email in Magento 2.3.x.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may refer the below manuals for more knowledge about ZF2 components:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/framework.zend.com\/manual\/2.4\/en\/modules\/zend.mime.part.html\" target=\"_blank\" rel=\"noreferrer noopener\">Zend\\Mime\\Part<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/framework.zend.com\/manual\/2.4\/en\/modules\/zend.mime.message.html\" target=\"_blank\" rel=\"noreferrer noopener\">Zend\\Mime\\Message<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/framework.zend.com\/manual\/2.4\/en\/modules\/zend.mail.message.html\" target=\"_blank\" rel=\"noreferrer noopener\">Zend\\Mail\\Message<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Attachments with Email in Magento 2.3.x:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1.  Add following code in <strong>di.xml<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you want to attach the file in&nbsp;<strong>frontend<\/strong>&nbsp;\u2013&nbsp;vendor\\module\\etc\\frontend\\di.xml<\/li>\n\n\n\n<li>If you want to attach the file in&nbsp;<strong>backend<\/strong>\u2013 vendor\\module\\etc\\adminhtml\\di.xml<\/li>\n\n\n\n<li>If you want to attach the file in&nbsp;<strong>both frontend and backend<\/strong>&nbsp;\u2013 vendor\\module\\etc\\di.xml<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n &lt;type name=\"Vendor\\Module\\Mail\\Template\\TransportBuilder\">\n        &lt;arguments>\n            &lt;argument name=\"message\" xsi:type=\"object\" >vendor\\module\\Mail\\Message&lt;\/argument>\n            &lt;argument name=\"messageFactory\" xsi:type=\"object\" >vendor\\module\\Mail\\MessageFactory&lt;\/argument>\n        &lt;\/arguments>\n &lt;\/type>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create <strong>Message.php<\/strong>&nbsp;file at <strong><strong>Vendor\\Module\\Mail\\Message.php<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Mail;\n\nuse Zend\\Mime\\Mime;\nuse Zend\\Mime\\PartFactory;\nuse Zend\\Mail\\MessageFactory as MailMessageFactory;\nuse Zend\\Mime\\MessageFactory as MimeMessageFactory;\nuse Magento\\Framework\\Mail\\MailMessageInterface;\n\nclass Message implements MailMessageInterface\n{\n    protected $partFactory;\n    protected $mimeMessageFactory;\n    private $zendMessage;\n    protected $parts = [];\n\n    public function __construct(\n        PartFactory $partFactory,\n        MimeMessageFactory $mimeMessageFactory,\n        $charset = 'utf-8'\n    )\n    {\n        $this->partFactory = $partFactory;\n        $this->mimeMessageFactory = $mimeMessageFactory;\n        $this->zendMessage = MailMessageFactory::getInstance();\n        $this->zendMessage->setEncoding($charset);\n    }\n\n    public function setBodyText($content)\n    {\n        $textPart = $this->partFactory->create();\n        $textPart->setContent($content)\n            ->setType(Mime::TYPE_TEXT)\n            ->setCharset($this->zendMessage->getEncoding());\n        $this->parts[] = $textPart;\n        return $this;\n    }\n\n    public function setBodyHtml($content)\n    {\n        $htmlPart = $this->partFactory->create();\n        $htmlPart->setContent($content)\n            ->setType(Mime::TYPE_HTML)\n            ->setCharset($this->zendMessage->getEncoding());\n        $this->parts[] = $htmlPart;\n        return $this;\n    }\n\n    public function setBodyAttachment($content, $fileName)\n    {\n        $fileType = Mime::TYPE_OCTETSTREAM;\n\n        $attachmentPart = $this->partFactory->create();\n        $attachmentPart->setContent($content)\n            ->setType($fileType)\n            ->setFileName($fileName)\n            ->setDisposition(Mime::DISPOSITION_ATTACHMENT)\n            ->setEncoding(Mime::ENCODING_BASE64);\n        $this->parts[] = $attachmentPart;\n        return $this;\n    }\n\n    public function setPartsToBody()\n    {\n        $mimeMessage = $this->mimeMessageFactory->create();\n        $mimeMessage->setParts($this->parts);\n        $this->zendMessage->setBody($mimeMessage);\n        return $this;\n    }\n\n    public function setBody($body)\n    {\n        return $this;\n    }\n\n    public function setSubject($subject)\n    {\n        $this->zendMessage->setSubject($subject);\n        return $this;\n    }\n\n    public function getSubject()\n    {\n        return $this->zendMessage->getSubject();\n    }\n\n    public function getBody()\n    {\n        return $this->zendMessage->getBody();\n    }\n\n    public function setFrom($fromAddress)\n    {\n        $this->zendMessage->setFrom($fromAddress);\n        return $this;\n    }\n\n    public function addTo($toAddress)\n    {\n        $this->zendMessage->addTo($toAddress);\n        return $this;\n    }\n\n    public function addCc($ccAddress)\n    {\n        $this->zendMessage->addCc($ccAddress);\n        return $this;\n    }\n\n    public function addBcc($bccAddress)\n    {\n        $this->zendMessage->addBcc($bccAddress);\n        return $this;\n    }\n\n    public function setReplyTo($replyToAddress)\n    {\n        $this->zendMessage->setReplyTo($replyToAddress);\n        return $this;\n    }\n\n    public function getRawMessage()\n    {\n        return $this->zendMessage->toString();\n    }\n\n    public function setMessageType($type)\n    {\n        return $this;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create <strong>MessageFactory.php<\/strong> file at <strong><strong>Vendor\\Module\\Mail\\MessageFactory.php<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Mail;\n\nclass MessageFactory extends \\Magento\\Framework\\Mail\\MessageInterfaceFactory\n{\n    \/**\n     * @var \\Magento\\Framework\\ObjectManagerInterface\n     *\/\n    protected $_objectManager;\n\n    \/**\n     * Factory constructor\n     *\n     * @param \\Magento\\Framework\\ObjectManagerInterface $objectManager\n     * @param string $instanceName\n     *\/\n    public function __construct(\\Magento\\Framework\\ObjectManagerInterface $objectManager, $instanceName = '\\\\Extait\\\\Attachment\\\\Mail\\\\Message')\n    {\n        $this->_objectManager = $objectManager;\n        $this->_instanceName = $instanceName;\n    }\n\n    \/**\n     * Create class instance with specified parameters\n     *\n     * @param array $data\n     * @return \\Magento\\Framework\\Mail\\MessageInterface\n     *\/\n    public function create(array $data = [])\n    {\n        return $this->_objectManager->create($this->_instanceName, $data);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create <strong>TransportBuilder.php<\/strong>&nbsp;file at <strong><strong>Vendor\\Module\\Model\\Mail\\Template\\TransportBuilder.php<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Model\\Mail\\Template;\n\nclass TransportBuilder extends \\Magento\\Framework\\Mail\\Template\\TransportBuilder\n{\n    public function addAttachment($fileString,$filename)\n    {\n        $arrContextOptions = [\n            \"ssl\" => [\n                \"verify_peer\" => false,\n                \"verify_peer_name\" => false,\n            ]\n        ];\n        \/* if $fileString is url of file *\/\n        $this->message->setBodyAttachment(file_get_contents($fileString, false, stream_context_create($arrContextOptions)), $filename);\n        \n        \/* if $fileString is string data *\/\n    $this->message->setBodyAttachment($fileString, $filename);\n        return $this;\n    }\n\n    protected function prepareMessage()\n    {\n        parent::prepareMessage();\n        $this->message->setPartsToBody();\n        return $this;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">5. Use <strong>TransportBuilder object<\/strong> in the file where you want to add an attachment in the Email.<br>For example Vendor\/Module\/Helper\/Data.php<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Helper;\n \nuse Magento\\Customer\\Model\\Session;\n \nclass Data extends \\Magento\\Framework\\App\\Helper\\AbstractHelper\n{\n    const XML_PATH_EMAIL_DEMO = 'emaildemo\/email\/email_demo_template';\n    \n    protected $inlineTranslation;\n    protected $transportBuilder;\n    protected $template;\n    protected $storeManager;\n \n    public function __construct(\n        \\Magento\\Framework\\App\\Helper\\Context $context,\n        \\Magento\\Framework\\Translate\\Inline\\StateInterface $inlineTranslation,\n        \\Vendor\\Module\\Model\\Mail\\Template\\TransportBuilder $transportBuilder,\n        \\Magento\\Store\\Model\\StoreManagerInterface $storeManager\n    ) \n    {\n        $this->inlineTranslation = $inlineTranslation;\n        $this->transportBuilder = $transportBuilder;\n        $this->storeManager = $storeManager;\n        parent::__construct($context);\n    }\n \n    public function generateTemplate()\n    {\n    \/\/ path for attachment File\n        $attachmentFile = 'file_path\/email.pdf';\n    $fileName = 'email.pdf';\n        \n        $emailTemplateVariables['message'] = 'This is a test message by meetanshi.';\n        \/\/load your email tempate\n        $this->_template  = $this->scopeConfig->getValue(\n            self::XML_PATH_EMAIL_DEMO,\n            \\Magento\\Store\\Model\\ScopeInterface::SCOPE_STORE,\n            $this->_storeManager->getStore()->getStoreId()\n        );\n        $this->_inlineTranslation->suspend();\n \n        $this->_transportBuilder->setTemplateIdentifier($this->_template)\n                ->setTemplateOptions(\n                    [\n                        'area' => \\Magento\\Framework\\App\\Area::AREA_FRONTEND,\n                        'store' => $this->_storeManager->getStore()->getId(),\n                    ]\n                )\n                ->setTemplateVars($emailTemplateVariables)\n                ->setFrom([\n                    'name' => 'Meetanshi',\n                    'email' => 'meetanshi@meetanshi.com',\n                ])\n                ->addTo('yourname@gmail.com', 'Your Name')\n                ->addAttachment($attachmentFile,$fileName); \/\/Attachment goes here.\n \n        try {\n            $transport = $this->_transportBuilder->getTransport();\n            $transport->sendMessage();\n            $this->_inlineTranslation->resume();\n        } catch (\\Exception $e) {\n            echo $e->getMessage(); die;\n        }\n    }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Attachments with Email in Magento 2.3.3:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create <strong>di.xml<\/strong> file at <strong><strong>app\/code\/vendor\/module\/etc\/<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;preference for=\"\\Magento\\Framework\\Mail\\Template\\TransportBuilder\" type=\"Vendor\\Module\\Model\\Mail\\Template\\TransportBuilder\" \/><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create <strong>TransportBuilder.php<\/strong> at <strong><strong>app\/code\/vendor\/module\/model\/mail\/template\/<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\ndeclare(strict_types=1);\n\nnamespace Vendor\\Module\\Model\\Mail\\Template;\n\nuse Magento\\Framework\\App\\TemplateTypesInterface;\nuse Magento\\Framework\\Exception\\LocalizedException;\nuse Magento\\Framework\\Exception\\MailException;\nuse Magento\\Framework\\Mail\\EmailMessageInterface;\nuse Magento\\Framework\\Mail\\EmailMessageInterfaceFactory;\nuse Magento\\Framework\\Mail\\AddressConverter;\nuse Magento\\Framework\\Mail\\Exception\\InvalidArgumentException;\nuse Magento\\Framework\\Mail\\MessageInterface;\nuse Magento\\Framework\\Mail\\MessageInterfaceFactory;\nuse Magento\\Framework\\Mail\\MimeInterface;\nuse Magento\\Framework\\Mail\\MimeMessageInterfaceFactory;\nuse Magento\\Framework\\Mail\\MimePartInterfaceFactory;\nuse Magento\\Framework\\Mail\\Template\\FactoryInterface;\nuse Magento\\Framework\\Mail\\Template\\SenderResolverInterface;\nuse Magento\\Framework\\Mail\\TemplateInterface;\nuse Magento\\Framework\\Mail\\TransportInterface;\nuse Magento\\Framework\\Mail\\TransportInterfaceFactory;\nuse Magento\\Framework\\ObjectManagerInterface;\nuse Magento\\Framework\\Phrase;\nuse Zend\\Mime\\Mime;\nuse Zend\\Mime\\PartFactory;\n\n\/**\n * TransportBuilder\n *\n * @api\n * @SuppressWarnings(PHPMD.CouplingBetweenObjects)\n * @since 100.0.2\n *\/\nclass TransportBuilder extends \\Magento\\Framework\\Mail\\Template\\TransportBuilder\n{\n    \/**\n     * Template Identifier\n     *\n     * @var string\n     *\/\n    protected $templateIdentifier;\n\n    \/**\n     * Template Model\n     *\n     * @var string\n     *\/\n    protected $templateModel;\n\n    \/**\n     * Template Variables\n     *\n     * @var array\n     *\/\n    protected $templateVars;\n\n    \/**\n     * Template Options\n     *\n     * @var array\n     *\/\n    protected $templateOptions;\n\n    \/**\n     * Mail Transport\n     *\n     * @var TransportInterface\n     *\/\n    protected $transport;\n\n    \/**\n     * Template Factory\n     *\n     * @var FactoryInterface\n     *\/\n    protected $templateFactory;\n\n    \/**\n     * Object Manager\n     *\n     * @var ObjectManagerInterface\n     *\/\n    protected $objectManager;\n\n    \/**\n     * Message\n     *\n     * @var EmailMessageInterface\n     *\/\n    protected $message;\n\n    \/**\n     * Sender resolver\n     *\n     * @var SenderResolverInterface\n     *\/\n    protected $_senderResolver;\n\n    \/**\n     * @var TransportInterfaceFactory\n     *\/\n    protected $mailTransportFactory;\n\n    \/**\n     * Param that used for storing all message data until it will be used\n     *\n     * @var array\n     *\/\n    private $messageData = [];\n\n    \/**\n     * @var EmailMessageInterfaceFactory\n     *\/\n    private $emailMessageInterfaceFactory;\n\n    \/**\n     * @var MimeMessageInterfaceFactory\n     *\/\n    private $mimeMessageInterfaceFactory;\n\n    \/**\n     * @var MimePartInterfaceFactory\n     *\/\n    private $mimePartInterfaceFactory;\n\n    \/**\n     * @var AddressConverter|null\n     *\/\n    private $addressConverter;\n\n    protected $attachments = [];\n\n    protected $partFactory;\n\n    \/**\n     * TransportBuilder constructor\n     *\n     * @param FactoryInterface $templateFactory\n     * @param MessageInterface $message\n     * @param SenderResolverInterface $senderResolver\n     * @param ObjectManagerInterface $objectManager\n     * @param TransportInterfaceFactory $mailTransportFactory\n     * @param MessageInterfaceFactory|null $messageFactory\n     * @param EmailMessageInterfaceFactory|null $emailMessageInterfaceFactory\n     * @param MimeMessageInterfaceFactory|null $mimeMessageInterfaceFactory\n     * @param MimePartInterfaceFactory|null $mimePartInterfaceFactory\n     * @param addressConverter|null $addressConverter\n     * @SuppressWarnings(PHPMD.UnusedFormalParameter)\n     * @SuppressWarnings(PHPMD.ExcessiveParameterList)\n     *\/\n    public function __construct(\n        FactoryInterface $templateFactory,\n        MessageInterface $message,\n        SenderResolverInterface $senderResolver,\n        ObjectManagerInterface $objectManager,\n        TransportInterfaceFactory $mailTransportFactory,\n        MessageInterfaceFactory $messageFactory = null,\n        EmailMessageInterfaceFactory $emailMessageInterfaceFactory = null,\n        MimeMessageInterfaceFactory $mimeMessageInterfaceFactory = null,\n        MimePartInterfaceFactory $mimePartInterfaceFactory = null,\n        AddressConverter $addressConverter = null\n    ) {\n        $this->templateFactory = $templateFactory;\n        $this->objectManager = $objectManager;\n        $this->_senderResolver = $senderResolver;\n        $this->mailTransportFactory = $mailTransportFactory;\n        $this->emailMessageInterfaceFactory = $emailMessageInterfaceFactory ?: $this->objectManager\n            ->get(EmailMessageInterfaceFactory::class);\n        $this->mimeMessageInterfaceFactory = $mimeMessageInterfaceFactory ?: $this->objectManager\n            ->get(MimeMessageInterfaceFactory::class);\n        $this->mimePartInterfaceFactory = $mimePartInterfaceFactory ?: $this->objectManager\n            ->get(MimePartInterfaceFactory::class);\n        $this->addressConverter = $addressConverter ?: $this->objectManager\n            ->get(AddressConverter::class);\n        $this->partFactory = $objectManager->get(PartFactory::class);\n        parent::__construct($templateFactory, $message, $senderResolver, $objectManager, $mailTransportFactory, $messageFactory, $emailMessageInterfaceFactory, $mimeMessageInterfaceFactory,\n            $mimePartInterfaceFactory, $addressConverter);\n    }\n\n    \/**\n     * Add cc address\n     *\n     * @param array|string $address\n     * @param string $name\n     *\n     * @return $this\n     *\/\n    public function addCc($address, $name = '')\n    {\n        $this->addAddressByType('cc', $address, $name);\n\n        return $this;\n    }\n\n    \/**\n     * Add to address\n     *\n     * @param array|string $address\n     * @param string $name\n     *\n     * @return $this\n     * @throws InvalidArgumentException\n     *\/\n    public function addTo($address, $name = '')\n    {\n        $this->addAddressByType('to', $address, $name);\n\n        return $this;\n    }\n\n    \/**\n     * Add bcc address\n     *\n     * @param array|string $address\n     *\n     * @return $this\n     * @throws InvalidArgumentException\n     *\/\n    public function addBcc($address)\n    {\n        $this->addAddressByType('bcc', $address);\n\n        return $this;\n    }\n\n    \/**\n     * Set Reply-To Header\n     *\n     * @param string $email\n     * @param string|null $name\n     *\n     * @return $this\n     * @throws InvalidArgumentException\n     *\/\n    public function setReplyTo($email, $name = null)\n    {\n        $this->addAddressByType('replyTo', $email, $name);\n\n        return $this;\n    }\n\n    \/**\n     * Set mail from address\n     *\n     * @param string|array $from\n     *\n     * @return $this\n     * @throws InvalidArgumentException\n     * @see setFromByScope()\n     *\n     * @deprecated 102.0.1 This function sets the from address but does not provide\n     * a way of setting the correct from addresses based on the scope.\n     *\/\n    public function setFrom($from)\n    {\n        return $this->setFromByScope($from);\n    }\n\n    \/**\n     * Set mail from address by scopeId\n     *\n     * @param string|array $from\n     * @param string|int $scopeId\n     *\n     * @return $this\n     * @throws InvalidArgumentException\n     * @throws MailException\n     * @since 102.0.1\n     *\/\n    public function setFromByScope($from, $scopeId = null)\n    {\n        $result = $this->_senderResolver->resolve($from, $scopeId);\n        $this->addAddressByType('from', $result['email'], $result['name']);\n\n        return $this;\n    }\n\n    \/**\n     * Set template identifier\n     *\n     * @param string $templateIdentifier\n     *\n     * @return $this\n     *\/\n    public function setTemplateIdentifier($templateIdentifier)\n    {\n        $this->templateIdentifier = $templateIdentifier;\n\n        return $this;\n    }\n\n    \/**\n     * Set template model\n     *\n     * @param string $templateModel\n     *\n     * @return $this\n     *\/\n    public function setTemplateModel($templateModel)\n    {\n        $this->templateModel = $templateModel;\n        return $this;\n    }\n\n    \/**\n     * Set template vars\n     *\n     * @param array $templateVars\n     *\n     * @return $this\n     *\/\n    public function setTemplateVars($templateVars)\n    {\n        $this->templateVars = $templateVars;\n\n        return $this;\n    }\n\n    \/**\n     * Set template options\n     *\n     * @param array $templateOptions\n     * @return $this\n     *\/\n    public function setTemplateOptions($templateOptions)\n    {\n        $this->templateOptions = $templateOptions;\n\n        return $this;\n    }\n\n    \/**\n     * Get mail transport\n     *\n     * @return TransportInterface\n     * @throws LocalizedException\n     *\/\n    public function getTransport()\n    {\n        try {\n            $this->prepareMessage();\n            $mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]);\n        } finally {\n            $this->reset();\n        }\n\n        return $mailTransport;\n    }\n\n    \/**\n     * Reset object state\n     *\n     * @return $this\n     *\/\n    protected function reset()\n    {\n        $this->messageData = [];\n        $this->templateIdentifier = null;\n        $this->templateVars = null;\n        $this->templateOptions = null;\n        return $this;\n    }\n\n    \/**\n     * Get template\n     *\n     * @return TemplateInterface\n     *\/\n    protected function getTemplate()\n    {\n        return $this->templateFactory->get($this->templateIdentifier, $this->templateModel)\n            ->setVars($this->templateVars)\n            ->setOptions($this->templateOptions);\n    }\n\n    \/**\n     * Prepare message.\n     *\n     * @return $this\n     * @throws LocalizedException if template type is unknown\n     *\/\n    protected function prepareMessage()\n    {\n        $template = $this->getTemplate();\n        $content = $template->processTemplate();\n        switch ($template->getType()) {\n            case TemplateTypesInterface::TYPE_TEXT:\n                $part['type'] = MimeInterface::TYPE_TEXT;\n                break;\n\n            case TemplateTypesInterface::TYPE_HTML:\n                $part['type'] = MimeInterface::TYPE_HTML;\n                break;\n\n            default:\n                throw new LocalizedException(\n                    new Phrase('Unknown template type')\n                );\n        }\n        $mimePart = $this->mimePartInterfaceFactory->create(['content' => $content]);\n        $parts = count($this->attachments) ? array_merge([$mimePart], $this->attachments) : [$mimePart];\n        $this->messageData['body'] = $this->mimeMessageInterfaceFactory->create(\n            ['parts' => $parts]\n        );\n\n        $this->messageData['subject'] = html_entity_decode(\n            (string)$template->getSubject(),\n            ENT_QUOTES\n        );\n        $this->message = $this->emailMessageInterfaceFactory->create($this->messageData);\n\n        return $this;\n    }\n\n    \/**\n     * Handles possible incoming types of email (string or array)\n     *\n     * @param string $addressType\n     * @param string|array $email\n     * @param string|null $name\n     *\n     * @return void\n     * @throws InvalidArgumentException\n     *\/\n    private function addAddressByType(string $addressType, $email, ?string $name = null): void\n    {\n        if (is_string($email)) {\n            $this->messageData[$addressType][] = $this->addressConverter->convert($email, $name);\n            return;\n        }\n        $convertedAddressArray = $this->addressConverter->convertMany($email);\n        if (isset($this->messageData[$addressType])) {\n            $this->messageData[$addressType] = array_merge(\n                $this->messageData[$addressType],\n                $convertedAddressArray\n            );\n        }\n    }\n\n    \/**\n     * @param string|null $content\n     * @param string|null $fileName\n     * @param string|null $fileType\n     * @return TransportBuilder\n     *\/\n    public function addAttachment(?string $content, ?string $fileName, ?string $fileType)\n    {\n        $attachmentPart = $this->partFactory->create();\n        $attachmentPart->setContent($content)\n            ->setType($fileType)\n            ->setFileName($fileName)\n            ->setDisposition(Mime::DISPOSITION_ATTACHMENT)\n            ->setEncoding(Mime::ENCODING_BASE64);\n        $this->attachments[] = $attachmentPart;\n\n        return $this;\n    }\n}  <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add attachment in email like :<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$this->transportBuilder->addAttachment($content, $fileName, 'application\/pdf');<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all you need to do for implementing Emails with attachments in Magento 2.3.x<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You may share the above solution with the Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-601","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=601"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions"}],"predecessor-version":[{"id":15268,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions\/15268"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}