Latheesan
Latheesan

Reputation: 24116

Magento Invoice sendEmail() with PDF attachment

I use the following code to load an invoice and send email programatically:

<?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;
?>

This appears to be sending the invoice email correctly. However, the PDF attachment of the invoice isn't there in the email.

If I were to send the email via magento, it works.

Any idea how to get the PDF to be attached, when calling sendEmail() function?

Upvotes: 2

Views: 2579

Answers (1)

Vertika Srivastava
Vertika Srivastava

Reputation: 167

For sending invoice email you need to overwrite

  1. In mage/core/model/email/template.php add this method at the end of the file:

    public function addAttachment(Zend_Pdf $pdf){
             $file = $pdf->render();
             $attachment = $this->getMail()->createAttachment($file);
             $attachment->type = 'application/pdf';
             $attachment->filename = 'test.pdf';
             }    
    

2 In sales/model/order/Invoice.php add the code between comments(2 lines of code) to the function sendEmail like this:

<?php 
public function sendEmail($notifyCustomer=true, $comment='')
        {
            if (!Mage::helper('sales')->canSendNewInvoiceEmail($this->getOrder()->getStore()->getId())) {
                return $this;
            }

            $currentDesign = Mage::getDesign()->setAllGetOld(array(
                'package' => Mage::getStoreConfig('design/package/name', $this->getStoreId()),
                'store'   => $this->getStoreId()
            ));

            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);

            $order  = $this->getOrder();
            $copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
            $copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $this->getStoreId());

            if (!$notifyCustomer && !$copyTo) {
                return $this;
            }

            $paymentBlock   = Mage::helper('payment')->getInfoBlock($order->getPayment())
                ->setIsSecureMode(true);

            $mailTemplate = Mage::getModel('core/email_template');

            if ($order->getCustomerIsGuest()) {
                $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $order->getStoreId());
                $customerName = $order->getBillingAddress()->getName();
            } else {
                $template = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $order->getStoreId());
                $customerName = $order->getCustomerName();
            }
            // attachment here
            $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($this));
            $mailTemplate->addAttachment($pdf);

            if ($notifyCustomer) {
                $sendTo[] = array(
                    'name'  => $customerName,
                    'email' => $order->getCustomerEmail()
                );
                if ($copyTo && $copyMethod == 'bcc') {
                    foreach ($copyTo as $email) {
                        $mailTemplate->addBcc($email);
                    }
                }
        // enter code here
            }

            if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
                foreach ($copyTo as $email) {
                    $sendTo[] = array(
                        'name'  => null,
                        'email' => $email
                    );
                }
            }

            foreach ($sendTo as $recipient) {
                $mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$order->getStoreId()))
                    ->sendTransactional(
                        $template,
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $order->getStoreId()),
                        $recipient['email'],
                        $recipient['name'],
                        array(
                            'order'       => $order,
                            'invoice'     => $this,
                            'comment'     => $comment,
                            'billing'     => $order->getBillingAddress(),
                            'payment_html'=> $paymentBlock->toHtml(),
                        )
                    );
            }

            $translate->setTranslateInline(true);

            Mage::getDesign()->setAllGetOld($currentDesign);

            return $this;
        }  ?>

Now when you create an invoice from the back office and you select to notify customer a pdf attachment should be sent as well.

Upvotes: 1

Related Questions