Click to See Complete Forum and Search --> : using mysql/php to create and email excel spreadsheet


thikho
08-26-2008, 04:31 AM
Hi All

I am trying to use the spreadsheet writer and php mailer at the same time i want to fetch data from a table and email it in a form of an attachment , the attachment must be in a form of a spreadsheet

My email is being sent successfully but i am not getting an attachment , pls help me out.here is my code

<?php

require_once ('paths.php');
require (CONNECTIONS.'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_array($selresult)) {
$email = $rec["Email_Address"];
$xlsName = "DocProc-results_".$email.".xls";

$xls = new Spreadsheet_Excel_Writer($xlsName);
$sheet =& $xls->addWorksheet('Doc Proc');

$sheet->setColumn(0, 0, 15);
$sheet->setColumn(2, 2, 80);

$sql = "SELECT Waybill_Date ,WaybillNumber, Customer_Reference, To_Ops_Branch, Route, Account_Number, Customer_Name, Service_Type, Scan_Back_Reason_Code, POD_Date, Date_Invoice_Received FROM Internal_Docs_waybills_outstanding";

$selres = mysql_query($sql);

$count = 0;

while($rec2=mysql_fetch_array($selres)) {
$sheet->write($count, 0, $rec2["WaybillNumber"]);

if ($rec2["Submitted"])
$sts = "Uploaded";
else
$sts = "Failed";

$sheet->write($count, 1, $sts);
$sheet->write($count, 2, $rec2["Waybill_Date"]);
$count++;
}

$xls->close();


$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "10.0.0.3";
$mail->From = "Thikho";
$mail->FromName = "Nikita";
$mail->Sender = "\"Nikita\" ";
$mail->AddAddress($email);
$mail->Subject = "Doc Proc Returned ";
$mail->Body = "Hi\n\nPlease find attached an Excel spreadsheet containing results for the returned Doc Proc.\n\nRegards\nNikita IT";
$mail->WordWrap = 50;
$mail->AddAttachment($xlsName);

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

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

}
}
?>

stephan.gerlach
08-27-2008, 06:29 AM
A while ago I had a similar problem. Then I figured out that I need to create a physical file on the server before emailing as attachment.

So what I did was created a folder in which I saved those files. And then I created a cron script that simply emptied the folder.

Hope this will help.

thikho
08-27-2008, 07:58 AM
How do you create a physical file on the server pls give me more info or links to tutorials as ai am a newbie on this , I have managed to create a modify my script and now i can be able to open and view my spreadsheet, the email goes thru but without the attachment