Magento:将pdf发票添加到发票电子邮件中

时间:2012-08-09 16:46:28

标签: php email magento invoice magento-1.7

过去几天我一直在寻找例子并且一直在尝试自己做,但是我坚持下面的事情。

我正在使用Magento 1.7.0.2。 我想在生成发票时将发票PDF作为附件添加到交易电子邮件中。

我尝试了几件事,包括更改/Mage/Sales/Model/Order/Invoice.php,/Mage/Core/Model/Email/Template.php和/Mage/Core/Model/Email/Template/Mailer.php

我知道这个过程涉及哪些文件,我知道在哪里添加附件,我只是无法弄清楚如何实际生成PDF并将其附加到电子邮件中。

似乎用magento 1.7.0.0改变了生成电子邮件的方式,所有解释都是针对1.6.x

我也知道有几个扩展(即FOOMAN),但我更喜欢自己更改文件(我希望尽可能地保持我的安装不受限制)。

2 个答案:

答案 0 :(得分:2)

我在网上搜索了几个小时,但解决方案是旧版Magento,我的版本是1.7.0.2。 Magento处理电子邮件的方式发生了变化。以下是我如何执行此操作的步骤,希望它对您有所帮助。

1)更改app / code / core / Mage / Core / Model / Email / Template / Mailer.php

Part 1, add protected $emailTemplate; to the top of the class:

class Mage_Core_Model_Email_Template_Mailer extends Varien_Object
{
/**
* List of email infos
* @see Mage_Core_Model_Email_Info
*
* @var array
*/
protected $_emailInfos = array();

// Add the following one line

protected $emailTemplate;

第2部分更新函数send()

public function send()
{

// the original was $emailTemplate = Mage::getModel('core/email_template');
// Change it to the following four lines:

if ($this->emailTemplate)
$emailTemplate = $this->emailTemplate;
else
$emailTemplate = Mage::getModel('core/email_template');

第3部分将函数添加到类的末尾:

public function addAttachment(Zend_Pdf $pdf, $filename){
$file = $pdf->render();

$this->emailTemplate = Mage::getModel('core/email_template');



$attachment = $this->emailTemplate->getMail()->createAttachment($file);
$attachment->type = 'application/pdf';
$attachment->filename = $filename;
}

2)更新app / code / core / Mage / Sales / Model / Order / Invoice.php

Update sendEmail function

$mailer = Mage::getModel('core/email_template_mailer');

// the next two lines were added

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



if ($notifyCustomer) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($order-

原始网址在这里: http://www.ericpan.com/2013/02/14/add-pdf-attachment-to-invoice-email/#.USPAKR2t2-0

答案 1 :(得分:0)

您可以查看

Mage_Adminhtml_Controller_Sales_Invoice

要查看正在生成的销售发票pdf的示例:

$pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf(array($invoice));
$this->_prepareDownloadResponse('invoice'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').
    '.pdf', $pdf->render(), 'application/pdf');

现在这实际上是将PDF包含在HTTP响应中,而不是将其保存为文件,但是在发票模型或抽象pdf模型中应该有一个方法来保存pdf。

然后,要添加附件,您可能需要查看:

Zend_Mail::addAttachment()

我没有一个方便的例子,实际上搜索了Magento 1.7以获取对addAttachment()的任何参考,但没有找到任何参考!有趣。

但希望这会给你一点方向。

相关问题