This extension also supports many payment methods (COD, check/money order, credit card, PayPal, etc.)
Magento 2 uses the TransportBuilder class to send emails, but it doesn’t support file attachments.​​​​​​​ 

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoFrameworkMailTemplateTransportBuilder" type="VendorModuleModelMailTransportBuilder" />

The Best Solution To Attach PDF File To Invoice Email

Writing is a part of my life and I’m living for it.
So in this article, we will show you how to programmatically attach a PDF file in your email!
So hereby, we want to introduce you to the best solution to attach PDF file to Magento 2 invoice emails:
<?php namespace VendorModuleModelSalesOrderEmail; use MagentoFrameworkMailTemplateTransportBuilder;
use MagentoFrameworkMailTemplateTransportBuilderByStore;
use MagentoSalesModelOrderEmailContainerIdentityInterface;
use MagentoSalesModelOrderEmailContainerTemplate; /**
* Email sender builder for attachments
class SenderBuilder extends MagentoSalesModelOrderEmailSenderBuilder
* @param Template $templateContainer
* @param IdentityInterface $identityContainer
* @param TransportBuilder $transportBuilder
* @param TransportBuilderByStore $transportBuilderByStore
* @param MagentoFrameworkFilesystemDriverFile $reader
public function __construct(
Template $templateContainer,
IdentityInterface $identityContainer,
TransportBuilder $transportBuilder,
TransportBuilderByStore $transportBuilderByStore = null,
VendorModuleHelperData $helper,
MagentoFrameworkFilesystemDriverFile $reader
) {
$this->helper = $helper;
$this->reader = $reader;
* Prepare and send email message
* @return void
public function send()
$this->configureEmailTemplate(); $this->transportBuilder->addTo(
); $copyTo = $this->identityContainer->getEmailCopyTo();
if (!empty($copyTo) && $this->identityContainer->getCopyMethod() == 'bcc') {
foreach ($copyTo as $email) {
} /* to attach events in invoice email */
$templateVars = $this->templateContainer->getTemplateVars();
$transport = $this->transportBuilder->getTransport();
if (!empty($templateVars['order'])) {
$order = $templateVars['order'];
foreach ($order->getAllItems() as $item) {
$data = $this->helper->createPdfFile($item, $order->getId());
if (!empty($data) && !empty($data['filename']) && !empty($data['pdfFile'])) {
// adds attachment in mail
$attachmentPart = $this->transportBuilder->addAttachment(
); $message = $transport->getMessage();
$body = ZendMailMessage::fromString($message->getRawMessage())->getBody();
$body = Zend_Mime_Decode::decodeQuotedPrintable($body);
$html = ''; if ($body instanceof ZendMimeMessage) {
$html = $body->generateMessage(ZendMailHeaders::EOL);
} elseif ($body instanceof MagentoFrameworkMailMimeMessage) {
$html = (string) $body->getMessage();
} elseif ($body instanceof MagentoFrameworkMailEmailMessage) {
$html = (string) $body->getBodyText();
} else {
$html = (string) $body;
} $htmlPart = new ZendMimePart($html);
$parts = [$htmlPart, $attachmentPart]; $bodyPart = new ZendMimeMessage();
} $transport->sendMessage();

Let’s learn it now!
CONTACT NOW to let us know your problems. We are willing to support you every time.
So to add email attachments programmatically, first of all, you need to create the new TransportBuilder class in app/code/Vendor/Module /Mail/Template/ TransportBuilder.php
Sometimes, you need to attach a PDF file to Magento 2 invoice email. However, manually attaching files is not feasible.

We hope this blog is helpful and good luck to you!

BSS Commerce is one of the leading Magento extension providers and web development services in the world. With experienced and certified Magento developers, we commit to bringing high-quality products and services to optimize your business effectively. Furthermore, we offer FREE Installation – FREE 1-year Support and FREE Lifetime Update for every Magento extension.
In this article, we have shown you how to programmatically attach a PDF file in your emails. 
Unlike most other modules that require you to install another third-party PDF invoice extension, this module already has a built-in PDF file to your invoice and shipment email.
Highlight features:

  • Automatically generate invoice and shipment based on payment methods for orders with a “new” order state
  • Automatically send invoice and shipment emails to customer
  • Apply automatic invoice and shipment for multiple payment methods


<?php namespace VendorModuleMailTemplate; class TransportBuilder extends MagentoFrameworkMailTemplateTransportBuilder
* addAttachment
* @param mixed $body
* @param string $filename
* @param mixed $mimeType
* @param mixed $disposition
* @param mixed $encoding
* @return object
public function addAttachment(
$filename = null,
$mimeType = Zend_Mime::TYPE_OCTETSTREAM,
$disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
$encoding = Zend_Mime::ENCODING_BASE64
) {
$attachmentPart = new ZendMimePart();
->setDisposition($disposition); return $attachmentPart;

Moreover, it helps you generate invoices and shipment automatically right after a customer finishes placing the order. This feature will save you a lot of time and effort, and enhance customer satisfaction.
Table of Contents
Add the SenderBuilder file in app/code/Vendor/Module/Model/Sales/Order/Email/SenderBuilder.php
Magento 2 Auto Invoice by BSS

We also introduced the easiest way to attach the file using Magento 2 extension.

Similar Posts