How to send image attachment with email template in magento 2

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

For Send Email Magento 2 without image attachment refer link,  Send Mail from Custom module Magento 2

For Override TransportBuilder.php file in Magento 2, you need to create di.xml file.
Using di.xml file you can override any PHP class in Magento 2.

Create di.xml in your module,
app/code/Rbj/ImageAttachement/etc/di.xml

Create TransportBuilder.php file at below location,

app/code/Rbj/ImageAttachement/Model/Mail/Template/TransportBuilder.php

Create helper file to send image attachment in mail template,

 

2 Replies to “How to send image attachment with email template in magento 2”

  1. Hello,

    In Magento 2.3.2, This code is not working. Follwoing error is occured

    Fatal error: Uncaught Error: Call to undefined method MagentoFrameworkMailMessageInterceptor::createAttachment() in /opt/lampp/htdocs/project/app/code/Custom/PersonalDataAccess/Model/Mail/Template/TransportBuilder.php:13 Stack trace: #0 /opt/lampp/htdocs/project/generated/code/Custom/PersonalDataAccess/Model/Mail/Template/TransportBuilder/Interceptor.php(24): CustomPersonalDataAccessModelMailTemplateTransportBuilder->addAttachment(‘xFFxD8xFFxE0x00x10JFIFx00x01x01x01x00…’, ‘image/jpeg’, ‘attachment’, ‘base64’, ‘preview_image_5…’) #1 /opt/lampp/htdocs/project/app/code/Custom/PersonalDataAccess/Helper/Email.php(81): CustomPersonalDataAccessModelMailTemplateTransportBuilderInterceptor->addAttachment(‘xFFxD8xFFxE0x00x10JFIFx00x01x01x01x00…’, ‘image/jpeg’, ‘attachment’, ‘base64’, ‘preview_image_5…’) #2 /opt/lampp/htdocs/project/app/code/Custom/PersonalDataAccess/Helper/Email.php(98): CustomPersonalDataAccessHelperEmail->generateTempl in /opt/lampp/htdocs/project/app/code/Custom/PersonalDataAccess/Model/Mail/Template/TransportBuilder.php on line 13

Leave a Reply