Hi

I have been playing round with php mailer and excel writer , i have been able to create my spreadsheet , but i need to email it as an attachment

I have created a new workbook and i have send it to the browser to check my result and it works fine but i have a problem with email it the message gets sent but there is not attachment

$mail->AddAttachment($workbook);

and i am trying to create a temp file for moving the spreadsheet first to my hard drive so that i can email it . i dont know if this is working properly

$mail->AddAttachment($workbook);
		if (!$mail->Send()) {
			echo $mail->ErrorInfo;			
		}

	try  {
		exec("mv ".$workbook." /var/www/test/excel/reports");
	}
	catch (Exception $err) {

	}

here is my code , your assistance on this will be highly appreciated

<?php
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

    require_once ('paths.php');
require (CONNECTIONS.'db_conn.php');

// processing finished, lets create the system logs (if there are any) and e-mail them
require_once ("Spreadsheet/Excel/Writer.php");
require (CLASSES.'class.phpmailer.php');
require(CLASSES.'class.smtp.php');

$sql = "SELECT DISTINCT(Email_Address) FROM Internal_Docs_email_users";
$selresult=mysql_query($sql);

while($rec=mysql_fetch_assoc($selresult)) {
	$email = $rec["Email_Address"];
	$xlsName = "DOC-results_".$email.".xls";

    $workbook = new Spreadsheet_Excel_Writer();
    $format_bold =& $workbook->addFormat();
    $format_bold->setBold();

    $worksheet =& $workbook->addWorksheet('DOC returned results');
    $worksheet->write(0, 0, "Branch", $format_bold);
    $worksheet->write(0, 1, "WayDate", $format_bold);
    $worksheet->write(0, 2, "WayNumber", $format_bold);
    $worksheet->write(0, 3, "Customer Reference", $format_bold);
$worksheet->write(0, 4, "Outstanding Days", $format_bold);


    # sfetch outstanding waybills
    $fullquery="SELECT Branch,Branch, Way_Date, WayNumber, Customer_Reference, POD_Date, NOW() AS today, (to_days(NOW()) - to_days(Way_Date)) AS total_days FROM Internal_Docs_waybills_outstanding";
    $result=mysql_query($fullquery);


    $i=1;
    while($row=mysql_fetch_array($result)){
            $worksheet->write($i, 0, "$row[Branch]");
            $worksheet->write($i, 1, "$row[Way_Date]");
            $worksheet->write($i, 2, "$row[WayNumber]");
            $worksheet->write($i, 3, "$row[Customer_Reference]");
	$worksheet->write($i, 4, "$row[total_days]");
            $i++;
    }
   	//$workbook->send('DocProcreturned.xls');
    $workbook->close();

$mail = new PHPMailer();
	$mail->IsSMTP();
	$mail->Host = "10.0.0.3";
	$mail->From = "testmail@thikho.co.za";
	$mail->FromName = "Thikho Web Services";
	$mail->Sender = "\"Thikho Web Services\" ";
	$mail->AddAddress($email);
	$mail->Subject = "Document Control System";
	$mail->Body = "Hi\n\nPlease find attached an Excel spreadsheet containing results for the Doc Proc Returned.\n\nRegards\nThikho Logistics IT";
	$mail->WordWrap = 50;
	$mail->AddAttachment($workbook);
	if (!$mail->Send()) {
		echo $mail->ErrorInfo;			
	}

	try  {
		exec("mv ".$workbook." /var/www/test/excel/reports");
	}
	catch (Exception $err) {

	}

}
?>

    You need to write the excel spreadsheet to the disk first. Then you need to attach it. Just pass in the path and file name to the constructor:

    $workbook = new Spreadsheet_Excel_Writer('/tmp/readyToMail.xls');

    Then after you do $workbook->close(); you can use "/tmp/readyToMail.xls" as the attachment name.

    I also notice you don't check the return value of addAttachment(). In their documentation they state that it will return false if there was an error. Perhaps you should check that when you try to attach a file.

    Here's their documentation on the addAttachment method:

    AddAttachment($path, $name = "", $encoding = "base64", $type = "application/octet-stream")

    string $path
    string $name
    string $encoding
    string $type

    Adds an attachment from a path on the filesystem. Returns false if the file could not be found or accessed.

      thank you very much i have some progress i wil let u know when i have managed to get it all rite
      I am getting my email and my attachment now , but when i download it the doc it say corrupted , i will have a look at it and get back to u
      tnks again

        Thank you very much bpat143 it iss working fine now , this what i did

        $

        xlsName = "/tmp/readytoMail-results.xls";
        
        $workbook = new Spreadsheet_Excel_Writer($xlsName);
        
        $mail->AddAttachment($xlsName);
          Write a Reply...