Igor Savinkin
Igor Savinkin

Reputation: 6277

Form a Excel file (PHPExcel) and mail it as an attachment (PHPMailer) without server storing, possible?

I want to form a file (PHPExcel) and mail it as an attachment (PHPMailer) without server storing, is it possible?

I'm aware of the possibility of forming/creating file in PHPExcel and sending it as an attachment thru PHPMailer here. But it works thru/by writing a file somewhere in a server. Poor as far as server resources consumption. PHPExcel allows to output this way directly without saving on a server:

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
ob_end_clean(); 
header('Content-Type: application/vnd.ms-excel');
...
$objWriter->save('php://output'); 

Is it possible (and how) to attach it to email on the fly similar as save('php://output')?

Upvotes: 0

Views: 4577

Answers (1)

Synchro
Synchro

Reputation: 37750

To send an attachment without using local files, use the addStringAttachment() method. For example:

$string = $mything->getExcelData();
$mail->addStringAttachment($string, 'myfile.xls');

Internally this will call PHPMailer::filenameToType(), which will set the appropriate application/vnd.ms-excel MIME type for the .xls extension.

In general you probably don't need to worry too much about memory consumption for doing this - PHPExcel itself is far more memory-intensive than simply storing this string temporarily, so if you are running into trouble, you will likely do so before you ever get this far

Upvotes: 1

Related Questions