Php – Magento: adding pdf invoice to invoice email

emailinvoicemagentomagento-1.7PHP

I have been searching for examples the last couple of days and have been trying to do it myself, but I'm stuck at the following.

I'm using Magento 1.7.0.2.
I want to add the invoice PDF as an attachment to the transactional email when generating the invoice.

I have tried several things including changing /Mage/Sales/Model/Order/Invoice.php, /Mage/Core/Model/Email/Template.php and /Mage/Core/Model/Email/Template/Mailer.php.

I know which files are involved in the process, I know where to add the attachment, I just can't figure out how to actually generate the PDF and attach it to the email.

It also seems that the way emails are generated has been changed with magento 1.7.0.0 and all explanations are for 1.6.x

I'm also aware that there are several extentions (i.e. FOOMAN's), but I prefer to change the files myself (I want to keep my installation clean of extentions as much as possible).

Best Answer

I searched online for a few hours but the solutions were for older version of Magento, my version is 1.7.0.2. And the way Magento handled email was changed. Here is the steps how I do this, hope it’s helpful for you.

1) Change app/code/core/Mage/Core/Model/Email/Template/Mailer.php

Part 1, add protected $emailTemplate; to the top of the class:

class Mage_Core_Model_Email_Template_Mailer extends Varien_Object
{
/**
* List of email infos
* @see Mage_Core_Model_Email_Info
*
* @var array
*/
protected $_emailInfos = array();

// Add the following one line

protected $emailTemplate;

Part 2 update the function send()

public function send()
{

// the original was $emailTemplate = Mage::getModel('core/email_template');
// Change it to the following four lines:

if ($this->emailTemplate)
$emailTemplate = $this->emailTemplate;
else
$emailTemplate = Mage::getModel('core/email_template');

Part 3 add function to the end of the class:

public function addAttachment(Zend_Pdf $pdf, $filename){
$file = $pdf->render();

$this->emailTemplate = Mage::getModel('core/email_template');



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

2) Update app/code/core/Mage/Sales/Model/Order/Invoice.php

Update sendEmail function

$mailer = Mage::getModel('core/email_template_mailer');

// the next two lines were added

$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($this));
$mailer->addAttachment($pdf,'invoice.pdf');



if ($notifyCustomer) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($order-

Th original url is here : http://www.ericpan.com/2013/02/14/add-pdf-attachment-to-invoice-email/#.USPAKR2t2-0

Related Topic