How to send image attachment with email template in Magento 2.4

Magento 2.4 Send Email with image attachment in the email template file.

This solution should work 2.4.* and a higher version.

For Attach an image in the email template, you need to override TransportBuilder Class from Magento\Framework\Mail\Template\TransportBuilder

For Send Custom Email with simple raw data refer link,  Send Mail from Custom module Magento 2

To Override the TransportBuilder class in Magento 2, you need to create a di.xml file. With this XML file, you can write syntax to override the Model class.

Create di.xml in your module,

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!-- Mail sending with attachment -->
    <preference for="Magento\Framework\Mail\Template\TransportBuilder" type="Rbj\ImageAttachement\Model\Mail\Template\TransportBuilder" />

Create TransportBuilder.php file at the below location, app/code/Rbj/ImageAttachement/Model/Mail/Template/TransportBuilder.php


namespace Rbj\ImageAttachement\Model\Mail\Template;

use Magento\Framework\Mail\Template\TransportBuilder as MagentoTransportBuilder;
use Zend_Mime;
use Zend_Mime_Part;

class TransportBuilder extends MagentoTransportBuilder
    private $parts = [];

    public function addAttachment(
        $filename = null,
        string $mimeType = Zend_Mime::TYPE_OCTETSTREAM,
        string $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
        string $encoding = Zend_Mime::ENCODING_BASE64
    ) {
        $mp = new Zend_Mime_Part($body);
        $mp->encoding = $encoding;
        $mp->type = $mimeType;
        $mp->disposition = $disposition;
        $mp->filename = $filename;
        $this->parts[] = $mp;

        return $this;

Create a helper class to send image attachments in a mail template,

namespace Rbj\ImageAttachement\Helper;
use Magento\Customer\Model\Session;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
    const EMAIL_IDENTIFIER_TEMPLATE = 'custommodule/general/emailtemplate';

    protected $_inlineTranslation;
    protected $_storeManager;

    protected $_transportBuilder;
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
    ) {
        $this->_inlineTranslation = $inlineTranslation;
        $this->_storeManager = $storeManager;
        $this->_transportBuilder = $transportBuilder;
    public function mailSend() {

        $storeId = $this->_storeManager->getStore()->getStoreId();
        $templateName = $this->scopeConfig->getValue(
        $senderName = 'Sender name'; //store admin sender name
        $senderEmail = ''; // store admin email id
        $recipientEmail = ''; // recipient email id
        $recipientName = 'recipient name';

        if (!$senderEmail && !$recipientEmail) {
            return false;


                'area'  => \Magento\Framework\App\Area::AREA_FRONTEND,
                'store' => $storeId,
                    'order' => 'custom order data',
                    'store' => $this->_storeManager->getStore(),
           /*  image attachment logic */
            $image = 'abc.jpg'; //actual image name
            $mediaPath = 'image_dynamic_file_path'.$image; //set image path
            $body = file_get_contents($mediaPath);
            $imageName = pathinfo($image,PATHINFO_BASENAME);

                'name'  => $senderName,
                'email' => $senderEmail,
            ->addTo($recipientEmail, $recipientName);

        /* @var \Magento\Framework\Mail\Transport $transport */
        $transport = $this->_transportBuilder->getTransport();

        try {
        } catch (\Exception $e) {