Attach PDF to Invoice Email in Magento 2 Programmatically
Running a Magento 2 store often means sending invoices to customers, and attaching a PDF copy directly to these emails is a common requirement. Doing this manually for every order can be time-consuming and inefficient. In this guide, we’ll show you how to attach a PDF invoice to email programmatically in Magento 2, so you can automate the process and save time. Let’s dive into the step-by-step instructions!
How to Attach PDF to Invoice Email in Magento 2
Step 1: Create a new module
To attach PDF invoices programmatically in Magento 2, the first step is to create a custom module. In this guide, we’ll name it Mageplaza_PdfAttachment.
- Register this module:
app/code/Mageplaza/PdfAttachment
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Mageplaza_PdfAttachment',
_DIR_
);
- Register this module:
app/code/Mageplaza/PdfAttachment/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mageplaza_PdfAttachment" setup_version="4.5.3">
</module>
</config>
Step 2: Register Mageplaza PDF Attachment plugin
Next, you need to register a plugin that will extend Magento’s default invoice email functionality. This plugin tells Magento to execute custom logic when sending invoice emails, allowing you to attach the PDF automatically.
Register this plugin: app/code/Mageplaza/PdfAttachment/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">
<type name="Magento\Sales\Model\Order\Email\Sender\InvoiceSender">
<plugin name="mageplaza_pdfattachment_invoice_sender" type="Mageplaza\PdfAttachment\Plugin\InvoiceSenderPlugin" />
</type>
</config>
Step 3: Create an Invoice Sender Plugin
Finally, you create the plugin class that generates the PDF and attaches it to the email.
Create this plugin: app/code/Mageplaza/PdfAttachment/Plugin/InvoiceSenderPlugin.php
<?php
namespace Mageplaza\PdfAttachment\Plugin;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Sales\Model\Order\Pdf\Invoice as InvoicePdf;
use Magento\Framework\Filesystem;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Magento\Sales\Model\Order\Invoice;
class InvoiceSenderPlugin
{
protected $pdfGenerator;
protected $fileSystem;
protected $directoryList;
protected $transportBuilder;
public function __construct(
InvoicePdf $pdfGenerator,
Filesystem $fileSystem,
DirectoryList $directoryList,
TransportBuilder $transportBuilder
) {
$this->pdfGenerator = $pdfGenerator;
$this->fileSystem = $fileSystem;
$this->directoryList = $directoryList;
$this->transportBuilder = $transportBuilder;
}
public function aroundSend(InvoiceSender $subject, callable $proceed, Invoice $invoice, $forceSyncMode = false)
{
$pdf = $this->pdfGenerator->getPdf([$invoice]);
$file = $this->directoryList->getPath(DirectoryList::TMP) . '/invoice_' . $invoice->getIncrementId() . '.pdf';
$pdf->render();
file_put_contents($file, $pdf->render());
$result = $proceed($invoice, $forceSyncMode);
$transport = $this->transportBuilder->getTransport();
$message = $transport->getMessage();
$attachment = $message->createAttachment(
file_get_contents($file),
'application/pdf',
\Zend_Mime::DISPOSITION_ATTACHMENT,
\Zend_Mime::ENCODING_BASE64,
'invoice_' . $invoice->getIncrementId() . '.pdf'
);
$attachment->filename = 'invoice_' . $invoice->getIncrementId() . '.pdf';
$transport->sendMessage();
@unlink($file);
return $result;
}
}
A Better Solution to Attach PDF File to Invoice Email
If you’re not an expert developer, the programmatic method may not be an ideal choice. Instead, using an extension would be easier. All the features are displayed as fields in the backend clearly for store owners to use.
Among the choices, we would like to introduce PDF Invoice for Magento 2. It can automatically send PDF Invoices to your customers via emails with essential information, including order details, payment methods, etc.
Highlight features:
- Compatible with API/GraphQL
- Provide attractive pre-made PDF templates
- Send PDF documents
- Offer various convenient payment methods
- Download and print professional PDF documents from My Order page
- Download and print PDF documents in mass
- Allow documents to be printed in customer and admin accounts
Learn more on how to use the extension here.
Final Word
Sending a PDF file with detailed order information to invoice emails would be solid proof of e-commerce dedication to customers. If you’re confident with coding skills, the programmatic method would be a good option. In case you’re not, that’s okay. Using the Magento extension can solve the problem since it’s much easier to use. For even more comprehensive customer communication, you can attach PDF invoice files directly to order confirmation emails, helping customers keep better track of their purchases.
Read more: 10+ Best Magento 2 PDF Invoice Extensions