e-mailing exported sql tables in excel file format

[php]

<?php include("class.phpmailer.php"); include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded $mail = new PHPMailer(); $body = file_get_contents('contents.html'); $body = eregi_replace("[\]",'',$body); $mail->IsSMTP(); $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port $mail->Username = "[email protected]"; // GMAIL username $mail->Password = "password"; // GMAIL password $mail->From = "[email protected]"; $mail->FromName = "Fund Tours Site Info"; $mail->Subject = "New Client Signup"; $mail->AltBody = "This is the body when user views in plain text format"; //Text Body $mail->WordWrap = 50; // set word wrap $mail->IsHTML(true); // send as HTML $mail->MsgHTML($body); $mail->AddReplyTo("[email protected]","Webmaster"); $mail->AddAttachment("file.xls"); // attachment $mail->AddAddress("[email protected]","First Last"); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message has been sent"; } ?>

[/php]

This code works by itself. But when combined with the rest of the script it doesn’t.

If paste this into a new…lets call it email.php and change my html form action to call email.php it emails the attachment just fine. but not when I paste it in with the rest of the code we have been working with.

Well, you can post the “combined” code and I will look at it. If you don’t want it all here in this post, send it to me in a personal message. I am sure it is just something that is being passed from the Excel parts…

But, off to bed soon!

Hey, I think I have it solved… In the Excel creation section… There is this near the end…

exit();

Nothing else will run after that, the code exits… Just remove that line…

Waiting...  Waiting...   Waiting...     Just wait'n to press the SOLVED button...  LOL  LOL

I am off to bed too…Here is the complete code:

[php]

<?php // This is my Insert to Database Code // define('DB_NAME', 'database'); define('DB_USER', 'username'); define('DB_PASSWORD', 'password'); define('DB_HOST', 'localhost'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db(DB_NAME, $link); if (!$db_selected) { die('Can\'t use ' . DB_NAME . ': ' . mysql_error()); } $value1 = $_POST['groupname']; $value2 = $_POST['name1']; $value3 = $_POST['name2']; $value4 = $_POST['address']; $value5 = $_POST['city']; $value6 = $_POST['state']; $value7 = $_POST['zip']; $value8 = $_POST['homephone']; $value9 = $_POST['cellphone']; $value10 = $_POST['email']; $value11 = $_POST['age']; $value12 = $_POST['maritalstatus']; $value13 = $_POST['income']; $value14 = $_POST['contact1']; $value15 = $_POST['contact2']; $value16 = $_POST['contact3']; $value17 = $_POST['date1']; $value18 = $_POST['date2']; $value19 = $_POST['date3']; $value20 = $_POST['gift']; $sql = "INSERT INTO clients (groupname, name1, name2, address, city, state, zip, homephone, cellphone, email, age, maritalstatus, income, contact1, contact2, contact3, date1, date2, date3, gift) VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '$value13', '$value14', '$value15', '$value16', '$value17', '$value18', '$value19', '$value20')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } mysql_close(); // This is my Excel File Creation Code // mysql_connect('localhost', 'username', 'password'); mysql_select_db('database'); $sql = "SELECT `groupname` AS `Group`, `name1` AS `Customer Name 1`, `name2` AS `Customer Name 2`, `address` AS `Address`, `city` AS `City`, `state` AS `State`, `zip` AS `Zip Code`, `homephone` AS `Home Phone`, `cellphone` AS `Cell Phone`, `email` AS `E-Mail`, `age` AS `Age Group`, `maritalstatus` AS `Marital Status`, `income` AS `Household Income`, `contact1` AS `Contact VIA`, `contact2` AS `Contact VIA`, `contact3` AS `Contact VIA`, `date1` AS `1st Date`, `date2` AS `2nd Date`, `date3` AS `3rd Date`, `gift` AS `Gift Choice` FROM fundtour_info.clients clients"; // Query Database $result=mysql_query($sql); $filename = 'file.xls'; // XLS Data Cell ob_start(); // start the file xlsBOF(); xlsWriteString(0,0,"Group"); xlsWriteString(0,1,"Name 1"); xlsWriteString(0,2,"Name 2"); xlsWriteString(0,3,"Address"); xlsWriteString(0,4,"City"); xlsWriteString(0,5,"State"); xlsWriteString(0,6,"Zip Code"); xlsWriteString(0,7,"Home Phone"); xlsWriteString(0,8,"Cell Phone"); xlsWriteString(0,9,"E-mail Address :"); xlsWriteString(0,10,"Age Group"); xlsWriteString(0,11,"Marital Status"); xlsWriteString(0,12,"Income"); xlsWriteString(0,13,"Contact Via"); xlsWriteString(0,14,"Dates"); xlsWriteString(0,15,"Gift Choice"); $xlsRow = 2; while(list($groupname, $name1, $name2, $address, $city, $state, $zip, $homephone, $cellphone, $email, $age, $maritalstatus, $income, $contact1, $contact2, $contact3,$date1, $date3, $date3, $gift)=mysql_fetch_row($result)) { ++$i; xlsWriteString($xlsRow,0,"$groupname"); xlsWriteString($xlsRow,1,"$name1"); xlsWriteString($xlsRow,2,"$name2"); xlsWriteString($xlsRow,3,"$address"); xlsWriteString($xlsRow,4,"$city"); xlsWriteString($xlsRow,5,"$state"); xlsWriteString($xlsRow,6,"$zip"); xlsWriteString($xlsRow,7,"$homephone"); xlsWriteString($xlsRow,8,"$cellphone"); xlsWriteString($xlsRow,9,"$email"); xlsWriteString($xlsRow,10,"$age"); xlsWriteString($xlsRow,11,"$maritalstatus"); xlsWriteString($xlsRow,12,"$income"); xlsWriteString($xlsRow,13,"$contact1, $contact2, $contact3"); xlsWriteString($xlsRow,14,"$date1, $date3, $date3"); xlsWriteString($xlsRow,15,"$gift"); $xlsRow++; } xlsEOF(); // $filename should be set to some writeable location file_put_contents($filename, ob_get_clean()); exit(); function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; } function xlsEOF() { echo pack("ss", 0x0A, 0x00); return; } function xlsWriteNumber($Row, $Col, $Value) { echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); echo pack("d", $Value); return; } function xlsWriteString($Row, $Col, $Value ) { $L = strlen($Value); echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); echo $Value; return; } // This is my Mail Code // include("class.phpmailer.php"); include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded $mail = new PHPMailer(); $body = file_get_contents('contents.html'); $body = eregi_replace("[\]",'',$body); $mail->IsSMTP(); $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port $mail->Username = "[email protected]"; // GMAIL username $mail->Password = "password"; // GMAIL password $mail->From = "[email protected]"; $mail->FromName = "Site Info"; $mail->Subject = "New Client Signup"; $mail->AltBody = "This is the body when user views in plain text format"; //Text Body $mail->WordWrap = 50; // set word wrap $mail->MsgHTML($body); $mail->AddReplyTo("[email protected]"); $mail->AddAttachment("file.xls"); // attachment $mail->AddAddress("[email protected]"); $mail->IsHTML(true); // send as HTML if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message has been sent"; } ?>

[/php]

Hope you got the other message… Remove the

exit();

… It is killing the PHP code at that point!

it worked…Hell ya!!!

before I close it, one more thing. This should be easy. instead of it saying “Message has been sent” when it is all finished I just want it to route them to form_complete.html. How can I do that.

Easy… Just replace the echo for the “mail is sent” with this…

header(“Location: somepage.php”);

Of course replace the page with your page. If it is in a folder do this:

header(“Location: somefoldername/somepage.php”);

If in a folder in a previous folder:

header(“Location: …/somefoldername/somepage.php”); (moves UP one folder and into another one…)

That should do it… Great it is now working… Congrats!

Ok. Everything is working perfect…

All I want to change is :
file_put_contents($filename, ob_get_clean());

I want it to save the file into a sub folder like public_html/info

Just add the folder in front of the name, like this:

file_put_contents("info/" . $filename, ob_get_clean());

This will send “info/file.xls” to the “file_put_contents” function… Should work for you…
(the . (period) concats the first string with the filename into one string…)

IT’S DONE!! Finally. Here is the final code. ErnieAlex you are a God among peasants sir. You saved my ass. I cannot thank you enough. but thank you, thank you , thank you.

[php]

<?php // This is my Insert to Database Code // define('DB_NAME', 'database'); define('DB_USER', 'username'); define('DB_PASSWORD', '[password'); define('DB_HOST', 'localhost'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysql_select_db(DB_NAME, $link); if (!$db_selected) { die('Can\'t use ' . DB_NAME . ': ' . mysql_error()); } $value1 = $_POST['groupname']; $value2 = $_POST['name1']; $value3 = $_POST['name2']; $value4 = $_POST['address']; $value5 = $_POST['city']; $value6 = $_POST['state']; $value7 = $_POST['zip']; $value8 = $_POST['homephone']; $value9 = $_POST['cellphone']; $value10 = $_POST['email']; $value11 = $_POST['age']; $value12 = $_POST['maritalstatus']; $value13 = $_POST['income']; $value14 = $_POST['contact1']; $value15 = $_POST['contact2']; $value16 = $_POST['contact3']; $value17 = $_POST['date1']; $value18 = $_POST['date2']; $value19 = $_POST['date3']; $value20 = $_POST['gift']; $sql = "INSERT INTO databasename (groupname, name1, name2, address, city, state, zip, homephone, cellphone, email, age, maritalstatus, income, contact1, contact2, contact3, date1, date2, date3, gift) VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '$value13', '$value14', '$value15', '$value16', '$value17', '$value18', '$value19', '$value20')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } mysql_close(); // This is my Excel File Creation Code // mysql_connect('localhost', 'username', 'password'); mysql_select_db('database'); $sql = "SELECT * FROM databasename.tablename tablename ORDER BY groupname ASC"; // Query Database $result=mysql_query($sql); $filename = 'filename.xls'; // XLS Data Cell ob_start(); // start the file xlsBOF(); xlsWriteString(0,0,"Group"); xlsWriteString(0,1,"Name 1"); xlsWriteString(0,2,"Name 2"); xlsWriteString(0,3,"Address"); xlsWriteString(0,4,"City"); xlsWriteString(0,5,"State"); xlsWriteString(0,6,"Zip Code"); xlsWriteString(0,7,"Home Phone"); xlsWriteString(0,8,"Cell Phone"); xlsWriteString(0,9,"E-mail Address :"); xlsWriteString(0,10,"Age Group"); xlsWriteString(0,11,"Marital Status"); xlsWriteString(0,12,"Income"); xlsWriteString(0,13,"Contact Via"); xlsWriteString(0,14,"Dates"); xlsWriteString(0,15,"Gift Choice"); $xlsRow = 2; while(list($groupname, $name1, $name2, $address, $city, $state, $zip, $homephone, $cellphone, $email, $age, $maritalstatus, $income, $contact1, $contact2, $contact3,$date1, $date3, $date3, $gift)=mysql_fetch_row($result)) { ++$i; xlsWriteString($xlsRow,0,"$groupname"); xlsWriteString($xlsRow,1,"$name1"); xlsWriteString($xlsRow,2,"$name2"); xlsWriteString($xlsRow,3,"$address"); xlsWriteString($xlsRow,4,"$city"); xlsWriteString($xlsRow,5,"$state"); xlsWriteString($xlsRow,6,"$zip"); xlsWriteString($xlsRow,7,"$homephone"); xlsWriteString($xlsRow,8,"$cellphone"); xlsWriteString($xlsRow,9,"$email"); xlsWriteString($xlsRow,10,"$age"); xlsWriteString($xlsRow,11,"$maritalstatus"); xlsWriteString($xlsRow,12,"$income"); xlsWriteString($xlsRow,13,"$contact1, $contact2, $contact3"); xlsWriteString($xlsRow,14,"$date1, $date3, $date3"); xlsWriteString($xlsRow,15,"$gift"); $xlsRow++; } xlsEOF(); // $filename should be set to some writeable location file_put_contents("info/" . $filename, ob_get_clean()); function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; } function xlsEOF() { echo pack("ss", 0x0A, 0x00); return; } function xlsWriteNumber($Row, $Col, $Value) { echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); echo pack("d", $Value); return; } function xlsWriteString($Row, $Col, $Value ) { $L = strlen($Value); echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); echo $Value; return; } // This is my Mail Code // include("class.phpmailer.php"); include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded $mail = new PHPMailer(); $body = file_get_contents('contents.html'); $body = eregi_replace("[\]",'',$body); $mail->IsSMTP(); $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port $mail->Username = "[email protected]"; // GMAIL username $mail->Password = "password"; // GMAIL password $mail->From = '[email protected]'; $mail->FromName = "Site Info"; $mail->Subject = "New Client Signup"; $mail->AltBody = "This is the body when user views in plain text format"; //Text Body $mail->WordWrap = 50; // set word wrap $mail->MsgHTML($body); $mail->AddReplyTo('[email protected]'); $mail->AddAttachment("filename.xls"); // attachment $mail->AddAddress("[email protected]"); $mail->IsHTML(true); // send as HTML if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { header("Location: thankyou.html"); } ?>

[/php]

What this script does now thanks to ErnieAlex…again thank you…is:

User inputs info into an html form and after submitting the form the script inserts the info into an sql database and table => then creates an xls(excel file) and saves it to you public_html folder=>e-mails the file as an attachment to a specified e-mail address=> and then finally sends you to a thank you page.

Thanks! Glad I could help… And, thank you too. I learned a lot about Excel creation. Never did much with that and was fun to help solve it…

We look forward to your next project… Congrats! CYA in the bitstream!

Sponsor our Newsletter | Privacy Policy | Terms of Service