7

I need to attach PDF and images with order email. I have dynamic PDF and image path which is stored in quote_item table. I have debugged code to attach image/PDF to order email.

<preference for="Magento\Framework\Mail\Template\TransportBuilder" type="vendor\moduleName\Model\Mail\MailTransportBuilder"></preference>
<preference for="Magento\Sales\Model\Order\Email\SenderBuilder" type="vendor\moduleName\Model\Mail\SenderBuilder"/>


<?php

namespace vendor\moduleName\Model\Mail;

class MailTransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
    public function addPdfAttachment($fileContent, $filename)
    {
        if ($fileContent) {
            $this->message->createAttachment(
                $fileContent,
                'application/pdf',
                \Zend_Mime::DISPOSITION_ATTACHMENT,
                \Zend_Mime::ENCODING_BASE64,
                $filename
            );

            return $this;
        }
    }

    public function addImageAttachment($fileContent, $filename)
    {
        if ($fileContent) {
            $this->message->createAttachment(
                $fileContent,
                \Zend_Mime::TYPE_OCTETSTREAM,
                \Zend_Mime::DISPOSITION_ATTACHMENT,
                \Zend_Mime::ENCODING_BASE64,
                $filename
            );

            return $this;
        }
    }

}


<?php
/**
 * @author     Kristof Ringleff
 * @package    Fooman_EmailAttachments
 * @copyright  Copyright (c) 2015 Fooman Limited (http://www.fooman.co.nz)
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace vendor\moduleName\Model\Mail;

use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;

class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{
    /**
     * @var Template
     */
    protected $templateContainer;

    /**
     * @var IdentityInterface
     */
    protected $identityContainer;

    /**
     * @var TransportBuilder
     */
    protected $transportBuilder;

    /**
     * @param Template $templateContainer
     * @param IdentityInterface $identityContainer
     * @param TransportBuilder $transportBuilder
     */
    public function __construct(
        Template $templateContainer,
        IdentityInterface $identityContainer,
        TransportBuilder $transportBuilder
    ) {
        $this->templateContainer = $templateContainer;
        $this->identityContainer = $identityContainer;
        $this->transportBuilder = $transportBuilder;
    }

    /**
     * Prepare and send email message
     *
     * @return void
     */
    public function send()
    {
        $this->transportBuilder->addPdfAttachment(file_get_contents($data),$data);

        parent::send();
    }


}

How can I inject order object in SenderBuilder class.

  • You should add more details to your question, add your related code, just offering bounty won't get you the answer – Piyush Jan 23 '18 at 13:49
2
+25

You can add attechement by preference also then add to etc/di.xml

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

Now you can use addAttachment() throughout your site.

<?php
namespace Vendor\Module\Mail\Template;

class TransportBuilder 
    extends \Magento\Framework\Mail\Template\TransportBuilder
{
    public function addAttachment(
        $body,
        $mimeType    = Zend_Mime::TYPE_OCTETSTREAM,
        $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
        $encoding    = Zend_Mime::ENCODING_BASE64,
        $filename    = null
    ) {
        $this->message->createAttachment($body, $mimeType, $disposition, $encoding, $filename);
        return $this;
    }
}
1

Magento's default email sending class TransportBuilder does not by default provides attachments in mail, but you can do it manually by overriding and using Zend framework integrated into Magento.

Here is a blog explaining the same - https://webkul.com/blog/attach-pdf-file-magento-2-email/

1

After research, I have found the way we can attach a dynamic image and PDF to order email. Kindly follow below steps to attach images to order email.

Step 1:

<preference for="Magento\Framework\Mail\Template\TransportBuilder" type="vendor\moduleName\Model\Mail\MailTransportBuilder"></preference>
<preference for="Magento\Sales\Model\Order\Email\SenderBuilder" type="vendor\moduleName\Model\Mail\SenderBuilder"/>
<preference for="Magento\Sales\Model\Order\Email\Sender\OrderSender" type="vendor\moduleName\Model\Mail\Sender\OrderSender"/>
<preference for="Magento\Sales\Model\Order\Email\Container\Template" type="vendor\moduleName\Model\Mail\Container\Template"/>

Step 2:

<?php

namespace vendor\moduleName\Model\Mail;

class MailTransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder
{
    public function addPdfAttachment($fileContent, $filename)
    {
        if ($fileContent) {
            $this->message->createAttachment(
                $fileContent,
                'application/pdf',
                \Zend_Mime::DISPOSITION_ATTACHMENT,
                \Zend_Mime::ENCODING_BASE64,
                $filename
            );

            return $this;
        }
    }

    public function addImageAttachment($fileContent, $filename)
    {
        if ($fileContent) {
            $this->message->createAttachment(
                $fileContent,
                \Zend_Mime::TYPE_OCTETSTREAM,
                \Zend_Mime::DISPOSITION_ATTACHMENT,
                \Zend_Mime::ENCODING_BASE64,
                $filename
            );

            return $this;
        }
    }

}

Step 3:

<?php
/**
 * @author     Kristof Ringleff
 * @package    Fooman_EmailAttachments
 * @copyright  Copyright (c) 2015 Fooman Limited (http://www.fooman.co.nz)
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace vendor\moduleName\Model\Mail;


class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{

    /**
     * Prepare and send email message
     *
     * @return void
     */
    public function send()
    {
        $ImageList = $this->templateContainer->getImageList();
        $PdfList = $this->templateContainer->getPdfList();

        if(is_array($ImageList)){              
                foreach ($ImageList as $key => $data) {
                        $this->transportBuilder->addImageAttachment(file_get_contents($data),$data);
                }
        }

        if(is_array($PdfList)){
                foreach ($PdfList as $key => $data) {
                        $this->transportBuilder->addPdfAttachment(file_get_contents($data),$data);
                }
        }

        parent::send();
    }


}

Step 4:

<?php
/**
 * @author     Kristof Ringleff
 * @package    Fooman_EmailAttachments
 * @copyright  Copyright (c) 2015 Fooman Limited (http://www.fooman.co.nz)
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace vendor\moduleName\Model\Mail\Sender;


class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender
{
    /**
     * @var \Fooman\EmailAttachments\Model\AttachmentContainerInterface
     */
    protected $templateContainer;
     protected $_designerhelper;

    public function __construct(
        \Magento\Sales\Model\Order\Email\Container\Template $templateContainer,
        \Magento\Sales\Model\Order\Email\Container\OrderIdentity $identityContainer,
        \Magento\Sales\Model\Order\Email\SenderBuilderFactory $senderBuilderFactory,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
        \Magento\Payment\Helper\Data $paymentHelper,
        \Magento\Sales\Model\ResourceModel\Order $orderResource,
        \Magento\Framework\App\Config\ScopeConfigInterface $globalConfig,
        \Magento\Framework\Event\ManagerInterface $eventManager
    ) {
        $this->templateContainer = $templateContainer;

        parent::__construct(
            $this->templateContainer,
            $identityContainer,
            $senderBuilderFactory,
            $logger,
            $addressRenderer,
            $paymentHelper,
            $orderResource,
            $globalConfig,
            $eventManager
        );
      /*  $this->attachmentContainer = $attachmentContainer;*/
    }

    public function send(\Magento\Sales\Model\Order $order, $forceSyncMode = false)
    {
        $items = $order->getAllVisibleItems();
        $IncrementId = $order->getIncrementId();
        $imageData = array();
        $pdfData = array();

        foreach ($items as $item) {

            if($item->getFilename() && $item->getDocumentId())
            {          
                       $imageData[] = $item->getImagePath();
                       $pdfData[] = $item->getPdfPath();
             }
        }

        if(count($pdfData) > 0){
                $this->templateContainer->setPdfList($pdfData);            
        }

        if(count($imageData) > 0){
                $this->templateContainer->setImageList($imageData);            
        }



        return parent::send($order, $forceSyncMode);
    }
}

Step 5

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace vendor\moduleName\Model\Mail\Container;

class Template extends \Magento\Sales\Model\Order\Email\Container\Template
{
    /**
     * @var array
     */
    protected $pdfAttach;

    /**
     * @var array
     */
    protected $imageAttach;

    public function setPdfList(array $pdfList)
    {
        $this->pdfAttach = $pdfList;
    }

    public function getPdfList()
    {
        return $this->pdfAttach;
    }

    public function setImageList(array $imageList)
    {
        $this->imageAttach = $imageList;
    }

    public function getImageList()
    {
        return $this->imageAttach;
    }


}
  • How can we attached file in custom email magento 2.3 ? because its using zendframework 2 – Manish Maheshwari Dec 7 '18 at 5:49

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.