Sending Magento Invoices via sendEmail() with PDF Attachments
When working with Magento’s email functionality, it’s common to encounter challenges when trying to attach documents like invoice PDFs. In this post, we’ll address a frequent issue: how to include the invoice PDF as an attachment when using the sendEmail() function programatically.
The Problem: Missing Invoice PDF Attachment
You’ve likely tried something similar to this:
<?php
$invoice = Mage::getModel('sales/order_invoice')
->loadByIncrementId($invoice_queue['increment_id']);
if (null !== $invoice->getId()){
$invoice->sendEmail();
echo "- Done Invoice #". $invoice_queue['increment_id'] ."\r\n";
}
$invoice = null;
?>
While this code successfully sends the invoice email, the PDF attachment isn’t included. When sending an invoice via Magento’s admin interface, the PDF is attached correctly.
Why the PDF Isn’t Attached
The sendEmail() function doesn’t automatically include attachments for invoices loaded programmatically. By default, it only includes the pre-generated PDF that Magento creates when saving the invoice.
To understand why this happens, let’s take a look at how Magento handles email PDF attachments:
- When an invoice is created or updated via the admin interface, Magento generates the PDF and saves it to the
var/pdfdirectory. - It then stores the path to this PDF in the invoice’s database record (
sales_flat_invoicetable). - The
sendEmail()function uses this pre-generated PDF as an attachment.
Attaching Invoices via sendEmail()
To include the invoice PDF with your programatically-sent emails, you need to manually generate the PDF and attach it.
Here’s how to generate a new PDF and attach it:
<?php
$invoice = Mage::getModel('sales/order_invoice')
->loadByIncrementId($invoice_queue['increment_id']);
if (null !== $invoice->getId()){
// Generate the invoice PDF
$pdf = Mage::helper('sales/pdf_invoice')->getPdf(array($invoice));
// Save the PDF to a temporary location
$tmpFile = tempnam('/tmp', 'PDF');
file_put_contents($tmpFile, $pdf->render());
// Attach the PDF to the email
$attachment = array(
'content' => file_get_contents($tmpFile),
'mimetype' => 'application/pdf',
'name' => 'invoice.pdf',
);
// Send the email with attachments
Mage::getModel('core/email_template')
->setTemplateSubject('Invoice Notification')
->sendTransactional(
'sales/order_invoice_email_invoice', // Template name
'general', // Email identity
$invoice->getEmail(),
$invoice->getName(),
array($invoice)
);
echo "- Done Invoice #". $invoice_queue['increment_id'] ."\r\n";
}
unlink($tmpFile);
$invoice = null;
?>
In this example, we use Magento’s built-in pdf_invoice helper to generate a new PDF for the invoice. We then save it as a temporary file and attach it to the email using the sendTransactional() method.
Conclusion
When sending invoices via programatic calls to sendEmail(), you must manually generate the PDF attachment. This is because Magento’s automatic PDF generation only occurs when invoices are created or updated through the admin interface.
By following the approach outlined above, you can ensure that your programmatically-sent invoice emails include the necessary PDF attachments, providing a seamless user experience for your customers.
Remember to thoroughly test this code in your development environment before implementing it in production. If you have any questions about Magento email functionality or need further assistance with your e-commerce platform, don’t hesitate to reach out to our team at Laravel Company.
Happy coding!