Magento 2.4.2 I am trying to send an attachment in the mail but getting Uncaught Error: Call to a member function createAttachment() on null in /Vendor/Module/Model/Mail/Template/TransportBuilder.php:10

di.xml file

 <?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="Vendor\Module\Model\Mail\Template\TransportBuilder" />

                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => $this->_storeManager->getStore()->getId(),
            ->addAttachment(file_get_contents($attachment)); //Attachment goes here 

transportbuilderfile attachment

namespace Vendor\Module\Model\Mail\Template;
use Zend_Mime;

class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
    public function addAttachment($body) {
//        $this->message->createAttachment((string)$body, 'application/pdf', \Zend_Mime::DISPOSITION_ATTACHMENT,
//           \Zend_Mime::ENCODING_BASE64, 'attachment.pdf');
        return $this;

Fallowed below links https://community.magento.com/t5/Magento-2-x-Programming/email-attachment-from-observer/td-p/77122


can any one tell me solution how to send pdf attachment

  • Because your not passing argument values and calling on null; Mar 2, 2022 at 14:01
  • @ZahidH can you check my update answer. which argument can you please tell me
    – Pawankumar
    Mar 2, 2022 at 14:14
  • It looks ok but try it if produce any error or exception paste here. Mar 2, 2022 at 14:36
  • @ZahidH PHP Fatal error: Uncaught Error: Call to a member function createAttachment() on null in /var/www/html/app/code/Vendor/Module/Model/Mail/Template/TransportBuilder.php:10
    – Pawankumar
    Mar 2, 2022 at 14:39
  • Make sure have you override correctly? did you added core class or dependency injection of Magento\Framework\Mail\Template in di.xml? also compile code correctly, also check error log Mar 2, 2022 at 15:05

1 Answer 1


I think the issue is just that you forgot to call the constructor class that's why you don't have the message variable initialized.

Constructor coming from


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)
    parent::__construct($templateFactory, $message, $senderResolver, $objectManager, $mailTransportFactory, $messageFactory, $emailMessageInterfaceFactory, $mimeMessageInterfaceFactory, $mimePartInterfaceFactory, $addressConverter);
  • I added but no use same error
    – Pawankumar
    Mar 2, 2022 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.