【问题标题】:How to attach a csv file to php email (code running from the magento root)如何将 csv 文件附加到 php 电子邮件(从 magento 根目录运行的代码)
【发布时间】:2014-01-03 12:59:51
【问题描述】:

我在 magento 根目录中有以下代码来发送电子邮件。

<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app("default");

$body = "Hi there, here is some body content";
$mail = Mage::getModel('core/email');
$mail->setToName('John');
$mail->setToEmail('ab@example.net');
$mail->setBody($body);
$mail->setSubject('The Subject');
$mail->setFromEmail('yourstore@url.com');
$mail->setFromName("Your Name");
$mail->setType('text');// You can use 'html' or 'text'

try {
    $mail->send();
    Mage::getSingleton('core/session')->addSuccess('Your request has been sent');

}
catch (Exception $e) {
    Mage::getSingleton('core/session')->addError('Unable to send.');

}

?>

如何在此电子邮件中附加多个 csv 文件。(至少 3 或 4 个)。

【问题讨论】:

    标签: php magento csv smtp sendmail


    【解决方案1】:

    你可以使用zend,这段代码已经过测试

    require 'app/Mage.php';
    umask( 0 );
    Mage :: app( "default" );
    $mail = new Zend_Mail();
    $mail->setType(Zend_Mime::MULTIPART_RELATED);
    $mail->setBodyHtml($html_body);
    $mail->setFrom('support@example.com', 'Example');
    $mail->addTo('dev.amitbera@gmail.com', 'Amit');
    $mail->setSubject('Sending email using Zend Framework');
    $dir = Mage::getBaseDir();
    $path = $dir.DS.'var'.DS.'docs'.DS.'test.csv';
    $file = $mail->createAttachment(file_get_contents($path));
    $file ->type        = 'text/csv';
    $file ->disposition = Zend_Mime::DISPOSITION_INLINE;
    $file ->encoding    = Zend_Mime::ENCODING_BASE64;
    $file ->filename    = 'test.csv';
    $mail->send();
    

    【讨论】:

      【解决方案2】:

      我认为他们在内部使用 PHPMailer,所以这真的很简单:

      $mail->addAttachment('csv/test.csv');
      

      【讨论】:

      • 我可以添加多个附件吗?
      • 是的,再次调用该函数即可。
      • 我收到以下错误未捕获异常 'Varien_Exception' 并带有消息 'Invalid method Mage_Core_Model_Email::addAttachment(Array ( [0] => csv/output.csv )
      • 好的,那么他们不支持该功能。你介意包含 PHPMailer (github.com/PHPMailer/PHPMailer) 吗?它支持这一点。
      【解决方案3】:
      $dir = Mage::getBaseDir();
      $file_name = $dir.DS.'var'.DS.'docs'.DS.'test.csv'; //file path
      if(file_exists($file_name))
      {
          $fileContents = file_get_contents($file_name);
          $fileName = 'test.csv';
          $mail->addAttachment($fileContents, $fileName);
      }
      

      【讨论】:

      • 我收到以下致命错误未捕获异常 'Varien_Exception' 并带有消息 'Invalid method Mage_Core_Model_Email::addAttachment
      • 等待我会在 1 小时内回复
      • Magento 'core/email' 模式无法发送附件。因此,您应该使用 core/email_template 模型。可以发送附件...如果您想发送此模型,那么它将提供代码
      • 请查看我最后的回答
      【解决方案4】:

      Magento 使用 zend_mail 框架工作。您可以调用 getMail 函数访问此框架以添加附件。

      http://framework.zend.com/manual/1.12/en/zend.mail.attachments.html

      例子:

      $transactionalEmail = Mage::getModel('core/email_template')>setDesignConfig(array('area'=>'frontend', 'store'=>1));
      
      $transactionalEmail->getMail()->createAttachment($fileContents,'text/csv')->filename =$filename;
      
      $transactionalEmail->sendTransactional($templateID, $sender, $email, "Admin", $vars);
      

      【讨论】:

      • 您能否解释一下答案最后一行中使用的变量的用法?什么是 $fileContents
      猜你喜欢
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 2012-12-14
      • 1970-01-01
      • 2011-08-14
      相关资源
      最近更新 更多