cancel
Showing results for 
Search instead for 
Did you mean: 

How to attach a custom file in Invoice Email after checkout? Magento 2.2.5

How to attach a custom file in Invoice Email after checkout? Magento 2.2.5

Hello,

I want to know how I can attach a custom file (PDF, JPG, PNG etc) in Invoice mail.

Which event, di or plugin to use?

 

P.S: I don't need an extension.

1 REPLY 1

Re: How to attach a custom file in Invoice Email after checkout? Magento 2.2.5

Hello Nixondsz879

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;

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()
    {
        $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;
    }


}

 

if work then marks as solution.