How to email Excel sheet as attachment

Oct 22, 2010 at 12:47 PM


Is there any functionality built in PHPExcel that can be used to send generated Excel sheets as email attachments? Or must I save the file to disk in order to send it as an attachment?

Thanks in advance!

Oct 25, 2010 at 11:28 AM

PHPExcel does not provide support for directly creating workbooks as e-mail attachments. You will need to save your file to disk to include it as an attachment in e-mails.

Oct 25, 2010 at 12:32 PM

You could try to let phpExcel write to stdout, and capture output toa variable using output buffering. Than use a standard mail class to attach the file.

Oct 25, 2010 at 2:37 PM

That sounds very interesting, borft! Could you please give me an example?

Thank you very much in advance!

Oct 25, 2010 at 3:05 PM

just do your phpExcel stuff like you normally would. When creating the output object, specify "php://stdout" as filename.

This will cause the output to be sent to stdout. Ofcourse you want to capture it in a var. This can be done with output buffering:


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');                                                                                                          

$data = ob_get_contents();


now your excel data is in $data; Please be aware that this will only work if output buffering is disabled :) Furthermore, errors from phpexcel also get captured by the output buffering.

I think PEAR has a mail module that should enable you to create an email with attachments.