Magento 2 : Dynamic PDF and image attachement with order email
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;
}
}
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;
}
}