如何将csv文件附加到php电子邮件(从magento root运行的代码)

时间:2014-01-03 12:59:52

标签: php magento csv smtp sendmail

我在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)。

4 个答案:

答案 0 :(得分:7)

您可以使用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();

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

$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);
}

答案 3 :(得分:0)

Magento使用zend邮件框架。您可以调用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);