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?
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.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
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.
Eg.
PHP Code:
<?php
$excel = new COM('Excel.Application');
Then start adding sheets, data, styling the columns and saving the file to the filesystem.
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 like to use PHPMailer and just call the applicable methods, because I'm lazy.
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.
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 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!
PHP Code:
<?php include_once('functions.php'); $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"; } else { $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/>"; echo "============================<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/>"; } else { $_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']; else $serialnum = '';
Bookmarks