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.

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

	}
}
?>

    does your mailer class except attachments that are vars? you may have to save it to a file first

      yes my mailer class accept attachment and i have tested it already by attaching a file from my c: drive and it works , but getting data from the database its a problem

      $mail->AddAttachment("c:/temp/thl.xls", "thl.xls");

      I am trying to modify my script and getting to learn more bout excel spreadsheet this script works perfectly It open an text.xls file with the info form my database but how do i make this info emailed the email still goes through but there is no attachment , anyone to help me pls

      <?php
              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');
      
      
          $workbook = new Spreadsheet_Excel_Writer();
          $format_bold =& $workbook->addFormat();
          $format_bold->setBold();
      
          $worksheet =& $workbook->addWorksheet();
          $worksheet->write(0, 0, "Branch", $format_bold);
          $worksheet->write(0, 1, "WaybillDate", $format_bold);
          $worksheet->write(0, 2, "WaybillNumber", $format_bold);
          $worksheet->write(0, 3, "Customer Reference", $format_bold);
          $worksheet->write(0, 4, "To_Ops_Branch", $format_bold);
      $worksheet->write(0, 5, "Route", $format_bold);
          $worksheet->write(0, 6, "Account Number", $format_bold);
          $worksheet->write(0, 7, "Customer Name", $format_bold);
          $worksheet->write(0, 8, "Service Type", $format_bold);
      $worksheet->write(0, 9, "Scan Back Reason Code", $format_bold);
          $worksheet->write(0, 10, "POD Date", $format_bold);
          $worksheet->write(0, 11, "Date Invoiced Received", $format_bold);
      
      
          # start by opening a query string
          $fullquery="select * 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[Waybill_Date]");
                  $worksheet->write($i, 2, "$row[WaybillNumber]");
                  $worksheet->write($i, 3, "$row[Customer_Reference]");
                  $worksheet->write($i, 4, "$row[To_Ops_Branch]");
      	$worksheet->write($i, 5, "$row[Route]");
                  $worksheet->write($i, 6, "$row[Account_Number]");
                  $worksheet->write($i, 7, "$row[Customer_Name]");
                  $worksheet->write($i, 8, "$row[Service_Type]");
      	$worksheet->write($i, 9, "$row[Sacn_Back_Reason_Code]");
                  $worksheet->write($i, 10, "$row[POD_Date]");
                  $worksheet->write($i, 11, "$row[Date_Invoiced_Received]");
                  $i++;
          }
          $workbook->send('test.xls');
          $workbook->close();
      ?>

      and someone told me to create a physical file on the server then run cron tab to empty the file . I am still searching on how to do that , pls your assistance will be highly appriciated

        a month later

        Hellow everyone,

        How do u create a physical file on the server before emailing as attachment and im not getting my attachment I have been searching on this and im not winning,

          Write a Reply...