Email Magento使用CSV发送帖子

Email Magento使用CSV发送帖子,email,magento,csv,attachment,Email,Magento,Csv,Attachment,我有一张表格可以通过电子邮件发送给店主。没什么特别的,只是一堆输入字段和选择字段 我正在尝试将其转换为CSV文件,并附加到电子邮件中 在我的控制器中,我有: public function postAction() { $post = $this->getRequest()->getPost(); if ($post) { #Mage::log($post,null,'tmp.log'); #not using this at

我有一张表格可以通过电子邮件发送给店主。没什么特别的,只是一堆输入字段和选择字段

我正在尝试将其转换为CSV文件,并附加到电子邮件中

在我的控制器中,我有:

    public function postAction() {
    $post = $this->getRequest()->getPost();
    if ($post) {
        #Mage::log($post,null,'tmp.log');

        #not using this at the moment...
        $postObject = new Varien_Object();
        $postObject->setData($post);


        $file = fopen('temp/temp.csv','w+');

        foreach($post as $sections) {
            fputcsv($file, $sections);
        };

        fclose($file);

        $mailTemplate = Mage::getModel('core/email_template')->loadDefault('training_email_template');
        $mailTemplate->setSenderName('Joe Schmoe');
        $mailTemplate->setSenderEmail('eat@joes.com');
        $mailTemplate->setTemplateSubject('Form Submission');
        $attachment = $mailTemplate->getMail()->createAttachment($file);
        $attachment->filename = 'file.csv';

        $mailTemplate->send(
            'store@owner.com',
            Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
            $post
        );
    }
}
现在文件本身被创建了,我可以很好地打开temp.csv,它看起来像是应该打开的。但是,附加的文件是空的,或者(当我尝试其他一些东西时)编码都不可靠。。。显然不是正确的格式


感觉我只是错过了一些简单的东西。。。有人有什么建议吗

您正在将一个文件资源作为参数发送到
createAttachment()
。它应该是一个字符串。试试这个:

$mailTemplate->getMail()->createAttachment(
    file_get_contents('temp/temp.csv'),
    Zend_Mime::TYPE_OCTETSTREAM,
    Zend_Mime::DISPOSITION_ATTACHMENT,
    Zend_Mime::ENCODING_BASE64,
    'file.csv'
);
这是您的文档


我在到处找那个函数的文档。。。我今天谷歌不及格。谢谢
/**  
 * Creates a Zend_Mime_Part attachment
 *
 * Attachment is automatically added to the mail object after creation. The
 * attachment object is returned to allow for further manipulation.
 *
 * @param  string         $body
 * @param  string         $mimeType
 * @param  string         $disposition
 * @param  string         $encoding
 * @param  string         $filename OPTIONAL A filename for the attachment
 * @return Zend_Mime_Part Newly created Zend_Mime_Part object (to allow
 * advanced settings)
 */
public function createAttachment($body,
                                 $mimeType    = Zend_Mime::TYPE_OCTETSTREAM,
                                 $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
                                 $encoding    = Zend_Mime::ENCODING_BASE64,
                                 $filename    = null)