Magento发票sendEmail()与PDF附件

时间:2013-08-21 12:13:41

标签: magento invoice

我使用以下代码加载发票并以编程方式发送电子邮件:

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

这似乎是正确发送发票电子邮件。但是,电子邮件中没有发票的PDF附件。

如果我要通过magento发送电子邮件,那就可以了。

在调用 sendEmail()函数时,知道如何附加PDF吗?

1 个答案:

答案 0 :(得分:1)

要发送发票电子邮件,您需要覆盖

  1. 在mage / core / model / email / template.php中,在文件末尾添加此方法:    

    public function addAttachment(Zend_Pdf $pdf){
             $file = $pdf->render();
             $attachment = $this->getMail()->createAttachment($file);
             $attachment->type = 'application/pdf';
             $attachment->filename = 'test.pdf';
             }    
    
  2. 2在sales / model / order / Invoice.php中,将注释之间的代码(2行代码)添加到sendEmail函数中,如下所示:

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

    现在,当您从后台创建发票并选择通知客户时,也应该发送pdf附件。