Magento – Attach invoice to shipment email

attachmentemailinvoicemagento-1.9

In /app/code/local/Mage/Sales/Model/Order/Shipment.php I added the following lines in sendEmail() funtion:

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

The file is correctly attached but the order items and invoice ID are missing.

For a test I added this in /app/code/local/Mage/Sales/Model/Order/Invoice.php :

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

This works 100% correctly.

Anybody see the problem?

Best Answer

I had the same request asked a while back. I used you question and your findings to solve our problem. I am posting in case someone else is in need of a solution.

I am using magento CE 1.9.1

The leg work:
"You never want to edit the core files always create an overwrite" and if you are really good create modules.

  1. move "app/code/core/Mage/Core/Model/Email/Template/Mailer.php" to "app/code/**local**/Mage/Core/Model/Email/Template/Mailer.php"

  2. move "/app/code/core/Mage/Sales/Model/Order/Shipment.php" to "/app/code/**local**/Mage/Sales/Model/Order/Shipment.php"

Coding:

Mailer.php

  1. add a protected variable before the first function declaration
    protected $emailTemplate;

  2. Modify the Send function

.

*// comment out this line:*
        // $emailTemplate = Mage::getModel('core/email_template');
*// add these lines:*
        if ($this->emailTemplate)
            $emailTemplate = $this->emailTemplate;
        else
            $emailTemplate = Mage::getModel('core/email_template');
  1. Add a function at the bottom of the file before closing the class

.

public function addAttachment($template, Zend_Pdf $pdf, $filename){
 $file = $pdf->render();
        $attachment = $template->getMail()->createAttachment($file);
        if(!is_object($attachment)) return $this;
        $attachment->type = 'application/pdf';
        $attachment->filename = $filename;
        return $this;
 }

Shipment.php

  1. below $storeId = $order->getStore()->getId(); add:

.

/* get invoice PDF */
$invoices = $order->getInvoiceCollection();
$pdfInvoice = Mage::getModel('sales/order_pdf_invoice');
$pdf = $pdfInvoice->getPdf($invoices);
  1. below $mailer = Mage::getModel('core/email_template_mailer'); add:

.

$mailer->addAttachment($pdf,'invoice.pdf');

I found my answer in Add PDF attachment to invoice email