attaching an excel sheet to an email
I'm using MIME to send a html email and i wanted to attach an excel sheet that will be generated from My SQL database to that email.
can you help me on how can i generate and attach that excel sheet to emails that my script sends?
This tute shows how to send attachments with html emails.
The tute attaches a zip file, but you should just need to change the mime type and file name to send a spreadsheet instead.
I like to use PHPMailer and just call the applicable methods, because I'm lazy. (Use the preceding link to download the PHP5-compatible version, as the link on their web site points to the PHP4 version.)
I'm guessing you also require the ability to create the Excel Spreadsheet using PHP?
Although I've never tried it fully, you can start by looking at something like COM.
Then start adding sheets, data, styling the columns and saving the file to the filesystem.
$excel = new COM('Excel.Application');
I tried the code above and it created an Excel instance in the background, obviously on my Windows computer. I only have working examples on creating spreadsheets using Visual Basic, but I'm sure the concept is the same with the PHP variant.
I'm lazy as well but rather than put up with any limitations a 3rd party application might have, I give the user a form with a tinyMCE text editor for them to write their html email content and the option to upload attachments. The code in the tute does the rest when the form data including target email addreses is submited to it.
Originally Posted by NogDog
This way I can customise the form and php script to however I like and then simply bolt it on to any other php application.
Thank you Trina for helping that, it is really helpful
thank you for responds
in the fallowing code i make a csv file but i have no idea if it works or how to attach it because in the cases above the file was already there!
$todaydate = date("Y-m-d");
$usersquery = mysql_query("SELECT * FROM users") or die(mysql_error);
$usersnumber = mysql_num_rows($usersquery);
if ($usersnumber == 0)
echo "there are no users registered in this website";
$cardsoverall = mysql_query("SELECT * FROM cards") or die(mysql_error);
$cardsoverallnum = mysql_num_rows($cardsoverall);
$startnum = ($cardsoverallnum + 1);
echo "You have " . $usersnumber . " user(s)<br/>";
echo "You have " . $cardsoverallnum . " card(s)<br/>";
echo "Excel starting number is: " . $startnum . "<br/>";
while($userarray = mysql_fetch_array($usersquery))
$name = $userarray['first'];
$email = '<html><body><strong>' . $name . ",</strong><br/><br/>" .
"<strong>Your cards have been processed, and added to your account.</strong><br/>" .
"<strong>Please sign into your 'Account Navigation' page. Please set the asking price(s) for your cards and view your account balance.</strong><br/><br/>" .
"<table rules='all' border='1' style='border-color: #666;' cellpadding='10'>" .
"<tr style='background: #eee;'><th><center><strong>Item Number</strong><center></th><th><center><strong>Description</strong></center></th></tr>";
$username = $userarray['username'];
echo $username . ": ";
$cards = mysql_query("SELECT * FROM cards WHERE title = '' AND seller = '$username'") or die("card finding error");
$cardsnumber = mysql_num_rows($cards);
if ($cardsnumber == 0)
echo "No new cards for this user<br/>";
$_file = 'Cardslist.csv';
$_fp = @fopen( $_file, 'w' );
$_csv_data= "Item Number" . "," . "Description" . "\n";
@fwrite( $_fp, $_csv_data );
while ($newcards = mysql_fetch_array($cards))
$cardid = $newcards['cardid'];
if ($newcards['sn'] != NULL)
$serialnum = " /" . $newcards['sn'];
$serialnum = '';
if ($newcards['cardnumber'] != NULL)
$title = $newcards['year'] . " " . $newcards['brand'] . " ". $newcards['setname'] . " #" . $newcards['cardnumber'] . " - " . $newcards['playername'] . $serialnum;
$title = $newcards['year'] . " " . $newcards['brand'] . " ". $newcards['setname'] . " - " . $newcards['playername'] . $serialnum;
$horizental = strtolower($newcards['horizontal']);
$backhorizental = strtolower($newcards['backhorizental']);
$seller = strtolower($newcards['seller']);
$updatecards = mysql_query("UPDATE cards SET title = '$title', horizontal = '$horizental', backhorizental = '$backhorizental', aday = '$todaydate', ava = 'yes', sold = 'no', send = 'no', shipping = 'no', seller = '$seller' WHERE cardid = '$cardid'") or die("changing information error");
echo "ERROR CARD " . $title . " with item number of " . $cardid . " had a problem!";
$email = $email . "<tr><td><center>" . $cardid . "</center></td><td><center>" . $title . "</center></td></tr>";
$_csv_data= $cardid . ',' . $title . "\n";
@fwrite( $_fp, $_csv_data );
$email = $email . "</table><br/><br/><strong>Your card processing receipt is attached in this email.<br/>If you have any comments, questions or concerns please feel free to contact us</strong>" .
"<br/><br/><strong>SCM</strong><br/>" . "<a href = 'http://www.scm.com'>scm.com</a>";
@fclose( $_fp );
$useremail = $userarray['email'];
$headers = "From: scm <firstname.lastname@example.org>" . "\r\n";
$headers .= "Reply-To: email@example.com" ."\r\n";
$headers .= 'Return-path: firstname.lastname@example.org' . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail ($useremail, 'Cards were added to your Account', $email, $headers, "-email@example.com"))
echo "E-mail was sent<br/>";
echo "error - e-mail didn't go through<br/>";
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)