I am working on a module in which I need to attach an attachment with sales email. However, Magento 2.3 use zendframework2 so email attachment is not the same as zendframework1 (in Magento 2.2).

Can anyone help me to achieve the same task? How to implement attachment functions in Magento 2.3

I have searched in google and read several questions but I didn't find the solution.


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;

I had the same issue.

To solve this i created a helper.

use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\Template\FactoryInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\Mail\TransportInterfaceFactory;   
use Magento\Framework\Mail\Template\TransportBuilder;

class UploadTransportBuilder extends TransportBuilder {

public function __construct(FactoryInterface $templateFactory,
    MessageInterface $message,
    SenderResolverInterface $senderResolver,
    ObjectManagerInterface $objectManager,
    TransportInterfaceFactory $mailTransportFactory) {

public function attachFile(
    $filename    = null,
    $mimeType    = \Zend_Mime::TYPE_OCTETSTREAM,
    $disposition = \Zend_Mime::DISPOSITION_ATTACHMENT,
    $encoding    = \Zend_Mime::ENCODING_BASE64
    $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
    return $this;


Then I loaded this on my controller.

use BVZ\Wishlist\Helper\UploadTransportBuilder;

UploadTransportBuilder $transportBuilder
$this->_transportBuilder = $transportBuilder

$transport = $this->_transportBuilder->setTemplateIdentifier('bvz_customer_wishlist')
            ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
            ->setTemplateVars(['data' => $postObject])
            ->addTo($formEmail, $customerName)
            ->attachFile($pdf,'Seleção de Produtos.pdf')

$this->messageManager->addSuccess(__('Um e-mail foi enviado com sua lista de desejo.'));


The code method for attach a file is attachFile();

  • Thank you @Gabriel Fernandes I will try this. Is it working in magento 2.3 ? – Sameer Bhayani Jan 8 at 13:39
  • i'm using 2.2.3, maybe works on 2.3 – Gabriel Fernandes Jan 8 at 13:42
  • Thanks @Gabriel. Magento 2.3 using zendframework2 & zendframework had deleted createAttachment() function so it is not working in 2.3 however older magento 2 version use zendframework1 so It is working in those versions error: prnt.sc/m4gr1e – Sameer Bhayani Jan 8 at 13:54

Send Email with Attachment in Magento 2.3



This solution is working so charming

 $transport = $_transportBuilder->setTemplateIdentifier(20)

 $html= $transport->getMessage()->getBody()->generateMessage();            
$bodyMessage = new \Zend\Mime\Part($html);
$bodyMessage->type = 'text/html';
$bodyPart = new \Zend\Mime\Message();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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