ylliX - Online Advertising Network Quicktip: How to send PDF invoice via transactional email in Magento? - Yet Another Programmer's Blog

Quicktip: How to send PDF invoice via transactional email in Magento?

Seems to be complex, but its not that bad:

$orderModel = Mage::getModel('sales/order')->load($myOrderId);
$invoices = $orderModel->getInvoiceCollection();
$invoicesSet = array();
foreach ($invoices as $_invoice) {
	array_push($invoicesSet, $_invoice);
}
$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoicesSet);
$file = $pdf->render();

$senderName = Mage::getStoreConfig('trans_email/ident_support/name');
$senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');
$sender = array('name' => $senderName, 'email' => $senderEmail);

$templateId = 'sales_email_order_template';
$emailTemplate = Mage::getModel('core/email_template')->loadByCode($templateId);
$vars = array('user_name' => 'some user name', 'product_name' => 'some product name');
$emailTemplate->getProcessedTemplate($vars);
$emailTemplate->setSenderEmail($senderEmail);
$emailTemplate->setSenderName($senderName);

$attachment = $emailTemplate->getMail()->createAttachment($file);
$attachment->type = 'application/pdf';
$attachment->filename = 'invoice.pdf';

$res = $emailTemplate->sendTransactional($templateId, $sender, 'user@example.com', "Admin", $vars);

About $vars variable – in this one you can put everything which is set inside email template as variables.

One thought on “Quicktip: How to send PDF invoice via transactional email in Magento?

Leave a Reply