/    Sign up×
Community /Pin to ProfileBookmark

using mysql/php to create and email excel spreadsheet

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

[code=php]<?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 = “HinnPlease find attached an Excel spreadsheet containing results for the returned Doc Proc.nnRegardsnNikita IT”;
$mail->WordWrap = 50;
$mail->AddAttachment($xlsName);

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

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

}
}
?> [/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@stephan_gerlachAug 27.2008 — 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.
Copy linkTweet thisAlerts:
@thikhoauthorAug 27.2008 — 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
×

Success!

Help @thikho spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...