2

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

我正在尝试将其转换为附加到电子邮件的 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,看起来应该是这样。但是,附加的文件是空的,或者(当我尝试了其他一些事情时)编码会很不稳定......显然不是正确的格式。

感觉我只是在这里遗漏了一些简单的东西......有人有什么建议吗?

4

1 回答 1

5

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

$mailTemplate->getMail()->createAttachment(
    file_get_contents('temp/temp.csv'),
    Zend_Mime::TYPE_OCTETSTREAM,
    Zend_Mime::DISPOSITION_ATTACHMENT,
    Zend_Mime::ENCODING_BASE64,
    'file.csv'
);

这是Zend_Mail的文档

/**  
 * 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)
于 2012-05-31T16:42:03.807 回答