2017-05-07 55 views
2

我想在SwiftMailer消息中附加Excel文件。将excel流附加到swiftmailer消息?

诀窍是我不想保存excel文件,然后附加它然后删除它,而我只是想生成excel并将其附加到消息。

这个功能可以附加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功能捆绑创建响应

/** 
* 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://输出保存在一个变量中或将其传递给newInstance方法?我尝试了$ response-> getContent()并将$ writer-> save('php:// output')传递给newInstance方法(我试过传递响应对象(StreamedResponse) 。

+0

这将是有用的? http://stackoverflow.com/questions/4279577/php-add-attachments-to-emails-on-the-fly – BentCoder

+0

我保存了php://输出ob_start()和ob_get_clean() –

回答

2

使用stream_get_contents

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

(不知道你的帽子实际的mimetype是)

相关问题