email - 将附件添加到订单电子邮件 + Magento

标签 email magento attachment

当客户下订单时,我需要将文件附加到 Magento 发送的电子邮件中。

此附件可以是 PDF、HTML 或简单的 TXT,并且必须包含订单摘要(SKU、数量、单价、总价)。

我怎样才能做到这一点?

提前致谢!

最佳答案

解决方案不是很复杂,尽管您需要一些时间来实现它。我将简要解释所需的所有步骤。

主要步骤是:

  • 在订单邮件中撰写附件并将其传递给邮件
  • 将其传输到电子邮件模板
  • 将其添加到作为附件发送的实际信件中

  • 1) 你需要重写Mage_Sales_Model_Order类(class)。覆盖该类中的 `sendNewOrderEmail()' 方法。

    您需要在此处编写要发送给客户的附件。复制原文sendNewOrderEmail()方法源代码添加到您的覆盖方法中,并将以下几行放在 $mailer->send() 之前(对于我们的示例,我们将采用简单的情况 - 我们将发送一个文本文件,其中仅包含订单的总计,附件将命名为“summary.txt”)
    $fileContents = "Hello, here is the copy of your invoice:\n";
    $fileContents .= sprintf("Grand total: %.2f", $this->getGrandTotal()) . "\n";
    $fileContents .= "Thank you for your visit!";
    $fileName = 'summary.txt';
    $mailer->addAttachment($fileContents, $fileName);
    

    2) 重写Mage_Core_Model_Email_Template_Mailer - 添加方法addAttachment($fileContents, $fileName) ,这会将传递的附件添加到 protected 变量中,该变量存储附件数组。

    覆盖 send()这个类中的方法。在该方法中,您需要将附件数组传递给每个发送的电子邮件模板。例如。添加行
    $emailTemplate->setAttachments($this->getAttachments());
    

    就在 $emailTemplate->setDesignConfig... 行之前

    3) 重写Mage_Core_Model_Email_Template .

    添加方法setAttachments($attachments) ,必须将传入的附件设置为某个 protected 变量。

    覆盖 send()这个类中的方法。在这种方法中,您需要在已发送的信件中添加附件。把线条像
    foreach ($this->getAttachments() as $atInfo) {
        $attachment = $mail->createAttachment($atInfo['fileContents']);
        $attachment->filename = $atInfo['fileName'];
    }
    

    就在 $mail->send() 之前在那里排队。

    就这样。对于 Magento 开发人员来说,完成这项任务并不难。它只是需要一些时间来编写内容、重写类和完成接口(interface)。

    关于email - 将附件添加到订单电子邮件 + Magento,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6662727/

    相关文章:

    e-commerce - 下订单后 Magento 自动发送电子邮件给用户

    java - 即使使用有效的 URI,图像也不会附加到文本消息中

    spring - Grails邮件插件:附加org.springframework.web.multipart.commons.CommonsMultipartFile

    node.js - 如何获取服务器ip地址以在nodejs中发送邮件

    Magento - 隐藏 "Subtotal"并仅显示/结账/一页/中的总计

    javascript - Mailto:IE 限制为 2048 个字符

    php - 模块 : failed to open stream: No such file or directory

    image - 通过文档文字 SOAP 在 base64 中发送图像的替代方法

    Javascript:验证警报

    php - filter_var($email, FILTER_VALIDATE_EMAIL) 在 if 条件下不起作用