3

I need to send via email an excell file after its creation, but un fortunately php don't have the rights to write in server, so I need to send it without save the file. And via browser it's not posible because you'll need to wait the creation of the excell and the database retrieving is very slow, so I wonder if there is a way to send it directly vía email. I've beeing googling for a while and haven't found a solution, thanks a lot!

edit to add code:

<?php

/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/');
/** PHPExcel */
include (dirname(__FILE__)."/../../lib/PHPExcel.php");
/** PHPExcel_Writer_Excel2007 */
include (dirname(__FILE__)."/../../lib/PHPExcel/Writer/Excel2007.php");
/** Php Mailer */
require (dirname(__FILE__).'/../PHPMailer/class.phpmailer.php');

require '../mysqlvars.php';
require '../lib/db.php';
require '../lib/form_functions.php';
require 'includes/session.php';
require("classes.php");
$dbConn = connectDB($dbHost, $dbUser, $dbPass, $dbDB);
if (!$dbConn) {
    die ('Cannot connect to database');
}

require 'includes/session.php';
require '../init.php';
require '../licence.php';


require("classes.php");
// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new PHPExcel();

// Set properties
echo date('H:i:s') . " Set properties\n";
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");


// Add some data
echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

// Rename sheet
echo date('H:i:s') . " Rename sheet\n";
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter->save();
print_r('ok');die; //never prints nothing
?>
Alfro
  • 1,524
  • 2
  • 21
  • 37
  • What mail library/function are you using? – gen_Eric Oct 19 '15 at 13:34
  • 1
    take a look on `base64_encode` function and example here http://stackoverflow.com/a/3093308/4421474 – Alex Oct 19 '15 at 13:34
  • can't you just send a link to the file for donwload? – Julio Soares Oct 19 '15 at 13:34
  • and now where is your mailing fragment? – Alex Oct 19 '15 at 13:35
  • cant save it on the browser because you'll nedd to wait un till the data base is retrived and the excell created, this take too much time. I nedd to do something more "automatic". Just to click a button to send the mail and leave the page – Alfro Oct 19 '15 at 13:36
  • my mail library is require (dirname(__FILE__).'/../PHPMailer/class.phpmailer.php'); – Alfro Oct 19 '15 at 13:36
  • I' haven't added the mail code because it's never called, except if I comment this line of code $objWriter->save(); and the mail is sent, it works flawlessly. My problem is I need to send the attached excell, not the mail itself – Alfro Oct 19 '15 at 13:37
  • 1
    PHPMailer can include base64 attachment as well as core php mail functions: http://stackoverflow.com/a/1202577/4421474 – Alex Oct 19 '15 at 13:39
  • Possible duplicate of [PHPMailer attachment, doing it without a physical file](http://stackoverflow.com/questions/11164167/phpmailer-attachment-doing-it-without-a-physical-file) – dognose Oct 19 '15 at 13:55

2 Answers2

7

Since you didn't provide your complete fragment of code here is my guess:

1st you should save file as/into variable and here is an example:

https://stackoverflow.com/a/9480344/4421474

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_start();
$objWriter->save('php://output');
$excelOutput = ob_get_clean();

after that you should attach that file as a BASE64 encoded string:

https://stackoverflow.com/a/1202577/4421474

Community
  • 1
  • 1
Alex
  • 16,739
  • 1
  • 28
  • 51
2

Based on the code from PHP_Excel on Github, you can use php://stdout to write the file to standard output.

So in theory you could use output buffering to capture the output file into a buffer.

e.g.

ob_start();
$objWriter->save('php://stdout');
$excelFileContents = ob_get_clean();

Note that further in the save() method it appears to use filesystem methods for the zip'ed portion of the file. This is untested.

You're then free to output this to the browser with the correct headers. According to [MSDN's MIME reference] (http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/office-2007-open-xml-mime-types.aspx) for Office 2007 that code might look like the following:

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
echo $excelFileContents;
exit; // Exit here to prevent trailing echo/debug output from being appended to the file.

If you use this approach you'll be limited by the runting memory limit so this might be an issue for larger files.

Edit: Instead of echoing the file direct since you're adding to an email you'll need to work with phpmailer in a similar fashion to append the file contents as a multipart/mixed email.

developerjack
  • 1,173
  • 6
  • 15