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) {
}
}
?>