2

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

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

4

1 回答 1

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 在 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 附件。

于 2015-01-31T09:16:05.363 回答