Monday 2 April 2012

Trying to add a PDF attachment to the order email

Trying to add a PDF attachment to the order email


I created a new module for this but you can overwrite directly in the magento files just to test if it works
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:

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);
                
}
            }

        }

        
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
 
 

No comments:

Post a Comment