Magento 2 Email Sending With Attachment

I will explain how to add attachment function with magento 2 so we can use that function anywhere as we wish.

First of all we will need custom module if not have we can create bsic module with module creator.


Second thing is we will need etc/di.xml in our custom module it will looks something like


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="\Magento\Framework\Mail\Template\TransportBuilder" type="\Namespace\CustomModule\Magento\Mail\Template\TransportBuilder" />
</config>

Next we need to extend \Magento\Framework\Mail\Template\TransportBuilder it will looks like

    <?php
    namespace Namespace\CustomModule\Magento\Mail\Template;
    
    class TransportBuilder 
        extends \Magento\Framework\Mail\Template\TransportBuilder
    {
        public function addAttachment(
            $body,
            $mimeType    = \Zend_Mime::TYPE_OCTETSTREAM,
            $disposition = \Zend_Mime::DISPOSITION_ATTACHMENT,
            $encoding    = \Zend_Mime::ENCODING_BASE64,
            $filename    = null
        ) {
            $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
            return $this;
        }

    }

That's it. we can Reuse anywhere as our need...!!!

Experienced Magento Certified Developer with a demonstrated history of working in the information technology and services industry. Skilled in SQL, PHP, XAMPP, jQuery, and Microsoft Office. Strong engineering professional with a Master of Computer Applications

1 comments:

  1. That's all right! You implementation is having value. Yet when i did research on email attachment for magento 2.3.0 i found that it requires ZF2 for meme part. Here's good info https://extait.com/blog/how-to-send-email-with-attachment-in-magento-2-3/

    ReplyDelete

Popular Posts

Have You Any Questins Let Us Know.

Contact Us
DENISH VACHHANI
Ahmedabad India