将Excel流附加到swiftmailer消息? [英] Attach excel stream to swiftmailer message?

查看:51
本文介绍了将Excel流附加到swiftmailer消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SwiftMailer消息中附加Excel文件.

I'm trying to attach an Excel file in a SwiftMailer message.

诀窍在于,我不想保存excel文件,然后将其附加然后删除,而我只想生成excel,并将其附加到邮件中.

The trick is that I don't want to save the excel file and then attach it and then delete it, instead I just want to generate the excel and attach that to the message.

此功能允许附加OutputByteStream

This function allows to attach a OutputByteStream

/**
 * Create a new Attachment.
 *
 * @param string|Swift_OutputByteStream $data
 * @param string                        $filename
 * @param string                        $contentType
 *
 * @return Swift_Mime_Attachment
 */
public static function newInstance($data = null, $filename = null, $contentType = null)
{
    return new self($data, $filename, $contentType);
}

Symfony PHPExcel捆绑包中有一个函数来创建响应

There is a function in Symfony PHPExcel bundle to create the response

/**
 * Stream the file as Response.
 *
 * @param \PHPExcel_Writer_IWriter $writer
 * @param int                      $status
 * @param array                    $headers
 *
 * @return StreamedResponse
 */
public function createStreamedResponse(\PHPExcel_Writer_IWriter $writer, $status = 200, $headers = array())
{
    return new StreamedResponse(
        function () use ($writer) {
            $writer->save('php://output');
        },
        $status,
        $headers
    );
}

他们似乎在呈现响应时调用了该回调,但是如何将php://output保存在变量中或将其传递给newInstance方法的东西?

They seems to call to that callback when they are rendering the reponse, but How can I save the php://output in a variable or something to pass it to newInstance method?

我尝试传递响应对象(StreamedResponse),但仅包含标头,我也尝试使用$ response-> getContent()并将$ writer-> save('php://output')传递给newInstance方法.

I tried passing the response object (StreamedResponse) but it only have the headers, I also tried with $response->getContent() and passing $writer->save('php://output') to newInstance method.

推荐答案

使用stream_get_contents

use stream_get_contents

$attachment = \Swift_Attachment::newInstance(stream_get_contents(createStreamedResponse(...)), 'test.xls', 'application/vnd.ms-excel');

(不知道您的实际模仿类型是什么)

(dont know hat your actual mimetype is)

这篇关于将Excel流附加到swiftmailer消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆