Magento 2.3+ – Attach a PDF on an email programmatically
Adding a custom made PDF inside an email would be something that you might need to develop, which is not standard contemplated by the core TransportBuilder Magento library, but don’t worry, this is not the end of the world, in this post am i going to show you how to proceed and get with an accurate solution.
0- You will need to create the PDF file. Follow this post to do so: Create PDF
1- Create your custom email template
app/code/Offset101/PdfEmail/etc/email_templates.xml
1 2 3 4 5 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd"> <template id="my_custom_email_with_pdf" label="My Custom Email" file="email_with_pdf.html" type="html" module="Offset101_PdfEmail" area="frontend"/> </config> |
app/code/Offset101/PdfEmail/view/frontend/email/email_with_pdf.html
1 2 3 4 |
<!--@subject My Custom Eamil With PDF @--> Hi there,<br> This is your custom email with PDF. Hope you enjoy it. |
2- Create your TransportBuilder class:
Offset101/PdfEmail/Model/Mail/TransportBuilder.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace Offset101\PdfEmail\Model\Mail\Template; class TransportBuilder extends \Magento\Framework\Mail\Template\TransportBuilder { 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; } } |
app/code/Offset101/PdfEmail/etc/di.xml
1 2 3 4 |
<?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="\Magento\Framework\Mail\Template\TransportBuilder" type="\Offset101\PdfEmail\Model\Mail\Template\TransportBuilder" /> </config> |
3- Attach the PDF and send the EMail.
app/code/Offset101/PdfEmail/Controller/Email/Share.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<?php namespace Offset101\PdfEmail\Controller\Email; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\Translate\Inline\StateInterface; use Offset101\PdfEmail\Model\Mail\Template\TransportBuilder; use Magento\Framework\App\Area; use Magento\Store\Model\StoreManagerInterface; use Zend\Mime\Mime; class Share extends Action { protected $inlineTranslation; protected $_transportBuilder; protected $_messageManager; protected $_dir; public function __construct( Context $context, StateInterface $inlineTranslation, TransportBuilder $transportBuilder, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\Filesystem\DirectoryList $dir ) { parent::__construct($context); $this->inlineTranslation = $inlineTranslation; $this->_transportBuilder = $transportBuilder; $this->_messageManager = $messageManager; $this->_dir = $dir; } public function execute() { $resultPage = $this->resultRedirectFactory->create(); $pdfFile = $this->_dir->getPath(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR).'/MyPDF.pdf'; if(!file_exists($pdfFile)) { // GENERATE PDF FILE IN THE SERVER IF NOT EXISTS } $sendTo = 'emailTo@test.com'; $this->inlineTranslation->suspend(); $transport = $this->_transportBuilder ->setTemplateIdentifier('my_custom_email_with_pdf') ->setTemplateOptions(['area' => Area::AREA_FRONTEND, 'store' => 1]) ->setTemplateVars( [ 'var1' => 'var2', 'var2' => 'var1' ]) ->setFrom('general') ->addTo($sendTo) ->getTransport(); $html = $transport->getMessage()->getBody()->generateMessage("\n"); $bodyMessage = new \Zend\Mime\Part(Mime::encode(quoted_printable_decode($html), 'utf-8', "\n")); $bodyMessage->type = 'text/html'; $attachment = $this->_transportBuilder->addAttachment(file_get_contents($pdfFile), 'MyPDF.pdf'); $bodyPart = new \Zend\Mime\Message(); $bodyPart->setParts(array($bodyMessage,$attachment)); $transport->getMessage()->setBody($bodyPart); $transport->sendMessage(); $this->inlineTranslation->resume(); $this->_messageManager->addSuccessMessage('You have successfully shared your PDF.'); return $resultPage->setPath('*/*/*'); } } |
Remember to redeploy the static content, clean cache, and compile your files. Afterwards you will be able to test.
Enjoy!