Can anyone help us on Add Attachment with email Magento 2.3? We have tried but face the same issue like this.

Fatal error: Uncaught Error: Call to undefined method Magento\Framework\Mail\Message\Interceptor::createAttachment() in Mail/TransportBuilder.php

Thank you in advance.


4 Answers 4



As from above link add encoding concept..

 public function setBodyAttachment($content, $fileName, $fileType)
    $attachmentPart = $this->partFactory->create();
        ->setEncoding(Mime::ENCODING_BASE64) /*Add this*/

    $this->parts[] = $attachmentPart;

    return $this;

I gave the advice there already. Try the method described here - https://extait.com/blog/how-to-send-email-with-attachment-in-magento-2-3/ as well as links provided to GitHub to see a sample module.


1) in your custom transport builder add the below function for atatchment:

<?php namespace Magebees\Demo\Model\Mail;

class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
     * @param Api\AttachmentInterface $attachment
    public function addAttachment($pdfString,$filename)
       $attachment = new \Zend\Mime\Part($pdfString);
            $attachment->type = \Zend_Mime::TYPE_OCTETSTREAM;
            $attachment->disposition = \Zend_Mime::DISPOSITION_ATTACHMENT;
            $attachment->encoding = \Zend_Mime::ENCODING_BASE64;
            $attachment->filename = $filename;
        return $attachment;

2) Then where you have used
$transport = $this->_transportBuilder->getTransport(); in that file add below code for send mail

$html='my testing html';            
            $bodyMessage = new \Zend\Mime\Part($html);
            $bodyMessage->type = 'text/html';
            $bodyPart = new \Zend\Mime\Message();

Fixed this issue by extending Magento\Framework\Mail\Message and adding new function createCustomAttachment in this, also edited the createHtmlMimeFromString function.

protected $attachment;

public function createCustomAttachment($body, $mimeType, $disposition, $encoding, $filename){
    $attachment = new Part($body);
    $this->attachment = $attachment;
    return $this;

Called the global variable $this->attachment in the function createHtmlMimeFromString. If the variable has value then we are adding the attachment data to the addPart function.

The code be like this

private function createHtmlMimeFromString($htmlBody)
    $htmlPart = new Part($htmlBody);
    $mimeMessage = new \Zend\Mime\Message();
    if ($this->attachment) {

    return $mimeMessage;

We need to copy the Magento\Framework\Mail\Message entire content in the extended file because the zendMessage is private and this is called in almost all functions.

We can call the createCustomAttachment function from the transport builder to pass the attachment details.

public function addAttachment($body,
                              $mimeType = Mime::TYPE_OCTETSTREAM,
                              $disposition = Mime::DISPOSITION_ATTACHMENT,
                              $encoding = Mime::ENCODING_BASE64,
                              $filename = null)
    //$this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
    $this->message->createCustomAttachment($body, $mimeType, $disposition, $encoding, $filename);
    return $this;

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.