2017-05-07 58 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是)

相關問題