njdirt Posted February 11, 2008 Share Posted February 11, 2008 Hello, I'm working on a code that will be run through a CronTab to pull bicycle repair orders together and send an e-mail with the next day's repairs listed. This is my first time building an e-mail script, and I'm having a problem getting the body of the email to appear properly. I have a long portion of HTML/PHP code to assemble an html page that lists the repairs. This coding has sections in php, and then formatting sections where the php is closed out and html is used, until I re-start php, etc. etc. I'd like to do something to the extent of take all of this and include it in the $body variable to appear in the mail as an html message body. The script below works properly, except that when run the page ends showing the message body, and the body appears empty in the e-mail. Any ideas what I can do to make this work? Thanks in advance! <?php $cur_date = date("F d, Y"); function report(){ include("includes/list.php"); ?> <html> <head> <title>Repair-Machine</title> </head> <body> <table border="0" width="75%" align="center"> <tr> <td> <h1>Repair Schedule for <?php echo $cur_date; ?><h1> </td> </tr> <tr> <td valign="top" width="100%"> <table border="1" width="100%"> <tr> <td colspan="5"> <h2>Todays Repairs</h2> </td> </tr> <tr> <td>Tag</td><td>Entry Date</td><td>Make</td><td>Model</td><td>Work Info</td> </tr> <?php ////////////TODAYS WORK & BUILDS////////////////////////////////////////// //get today's start stamp $begin = time(); $begin_day = mktime(0, 0, 0, date('m', $begin),date('d', $begin),date('Y', $begin)); $query2 = "SELECT * FROM $table4 WHERE est_completion='$begin_day' ORDER BY rid ASC"; $result2 = mysql_db_query ($dbname, $query2, $link); $row_number = "1"; while($row2 = mysql_fetch_array($result2)){ $row_color = ($row_count % 2) ? $color1 : $color2; if ($row2[closed] != "yes"){ $row_color = $color_open; } $bid = $row2[bid]; $cid = $row2[cid]; //see if there is a part on order $query8 = "SELECT status FROM $table5 WHERE cid='$cid' AND rid='$row2[rid]'"; $result8 = mysql_db_query ($dbname, $query8, $link); $row8 = mysql_fetch_array($result8); if ($row8[status] == "0"){ $row_color = $color_order; } if ($row8[status] == "1"){ $row_color = $color_ship; } //get bike info $query3 = "SELECT * FROM $table3 WHERE bid='$bid' LIMIT 1"; $result3 = mysql_db_query ($dbname, $query3, $link); $row3 = mysql_fetch_array($result3); //get ticket id number $query13 = "SELECT tid FROM $table12 WHERE rid='$row2[rid]'"; $result13 = mysql_db_query ($dbname, $query13, $link); $row13 = mysql_fetch_array($result13); if ($row13[tid]==""){ $tid="0"; }else{ $tid=$row13[tid]; } //format date $origin_date = date("F d, Y", $row2[origin_date]); //capitalize first letters in strings $make = ucfirst($row3[brand]); $model = ucfirst($row3[model]); $work = ucfirst($row2[work_needed]); echo "<tr><td><b>$tid</b></td><td>$origin_date</td><td >$make</td><td>$model</td><td>$work</td></tr>"; //print out the work information $work_order = "<b>Work Order:</b> $row2[work_needed]"; $extra_info = "<b>Extra Information:</b> $row2[work_extra_info]"; $orig = date("D F d, Y", $row2[origin_date]); $origin_date = "<b>Origin Date:</b> $orig"; $sign_in_person = "<b>Sign-In Person:</b> $row2[est_person]"; echo "<tr><td></td><td colspan=\"4\">$work_order<br>$extra_info<br>$origin_date<br>$sign_in_date</td></tr>"; $row_count++; $row_number++; } ?> <tr> <td colspan="5"> <h2>Todays Bike Builds</h2> </td> </tr> <tr> <td>No.</td><td>Frame Size</td><td>Make</td><td>Model</td><td>Serial Number</td> </tr> <?php //begin displaying build orders in table $query11 = "SELECT sid FROM $table6a WHERE build_date='$begin_day' ORDER BY sid ASC"; $result11 = mysql_db_query ($dbname, $query11, $link); $row_number = "1"; while($row11 = mysql_fetch_array($result11)){ $row_color = $color_build; //get bike info $query12 = "SELECT * FROM $table9 WHERE sid='$row11[sid]' LIMIT 1"; $result12 = mysql_db_query ($dbname, $query12, $link); $row12 = mysql_fetch_array($result12); echo "<tr><td>$row_number</td><td>$row12[frame_size]</td><td>$row12[make]</td><td>$row12[model]</td><td>$row12[serial]</td></tr>"; $row_number++; } ?> </table><BR><BR><BR> </td> </tr> <tr> <td valign="top" width="100%"> <table width="100%" border="1"> <tr> <td colspan="5"> <h2>Overdue Repairs</h2> </td> </tr> <tr> <td>Tag</td><td>Entry Date</td><td>Make</td><td>Model</td><td>Work Info</td> </tr> <?php //////////////////OVERDUE WORK////////////////////////////////////// //get today's start stamp $begin = time(); $begin_day = mktime(0, 0, 0, date('m', $begin),date('d', $begin),date('Y', $begin)); $query4 = "SELECT * FROM $table4 WHERE est_completion<'$begin_day' AND closed!='yes' ORDER BY rid ASC"; $result4 = mysql_db_query ($dbname, $query4, $link); $row_number = "1"; while($row4 = mysql_fetch_array($result4)){ $row_color = ($row_count % 2) ? $color1 : $color2; $bid = $row4[bid]; $cid = $row4[cid]; //see if there is a part on order $query9 = "SELECT status FROM $table5 WHERE cid='$cid' AND rid='$row4[rid]'"; $result9 = mysql_db_query ($dbname, $query9, $link); $row9 = mysql_fetch_array($result9); if ($row9[status] == "0"){ $row_color = $color_order; } if ($row9[status] == "1"){ $row_color = $color_ship; } //get bike info $query5 = "SELECT * FROM $table3 WHERE bid='$bid' LIMIT 1"; $result5 = mysql_db_query ($dbname, $query5, $link); $row5 = mysql_fetch_array($result5); //get ticket id number $query14 = "SELECT tid FROM $table12 WHERE rid='$row4[rid]'"; $result14 = mysql_db_query ($dbname, $query14, $link); $row14 = mysql_fetch_array($result14); if ($row14[tid]==""){ $tid="0"; }else{ $tid=$row14[tid]; } //format date $origin_date = date("F d, Y", $row4[origin_date]); //capitalize first letters $make = ucfirst($row5[brand]); $model = ucfirst($row5[model]); $work = ucfirst($row4[work_needed]); echo "<tr><td><b>$tid</b>></td><td>$origin_date</td><td>$make</td><td>$model</td><td>$work</td></tr>"; //print out the work information $work_order = "<b>Work Order:</b> $row5[work_needed]"; $extra_info = "<b>Extra Information:</b> $row5[work_extra_info]"; $orig = date("D F d, Y", $row5[origin_date]); $origin_date = "<b>Origin Date:</b> $orig"; $sign_in_person = "<b>Sign-In Person:</b> $row5[est_person]"; echo "<tr><td></td><td colspan=\"4\">$work_order<br>$extra_info<br>$origin_date<br>$sign_in_date</td></tr>"; $row_count++; $row_number++; } ?> <tr> <td colspan="5"> <h2>Overdue Bike Builds</h2> </td> </tr> <tr> <td>No.</td><td>Frame Size</td><td>Make</td><td>Model</td><td>Serial Number</td> </tr> <?php //begin displaying build orders in table $query11 = "SELECT sid FROM $table6a WHERE build_date<'$begin_day' ORDER BY sid ASC"; $result11 = mysql_db_query ($dbname, $query11, $link); $row_number = "1"; while($row11 = mysql_fetch_array($result11)){ $row_color = $color_build; //get bike info $query12 = "SELECT * FROM $table9 WHERE sid='$row11[sid]' LIMIT 1"; $result12 = mysql_db_query ($dbname, $query12, $link); $row12 = mysql_fetch_array($result12); echo "<tr><td>$row_number</td><td>$row12[frame_size]</td><td>$row12[make]</td><td >$row12[model]</td><td>$row12[serial]</td></tr>"; $row_number++; } ?> </table> </body> </html> <?php } //send the actual e-mail $to = "somebody@gmail.com"; $subject = "Repair Machine Schedule for $cur_date"; $body = report(); $headers = "From: Repair.Machine@internet.com"; if (mail($to, $subject, $body, $headers)) { echo("<p>Message successfully sent!</p>"); } else { echo("<p>Message delivery failed...</p>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90555-how-to-compile-e-mail-body-from-assorted-htmlphp-code/ Share on other sites More sharing options...
sKunKbad Posted February 12, 2008 Share Posted February 12, 2008 Just to test, if you run the script with echo report(), does it show the expected output? Also, if you send a simple email replacing the $body = report() with $body = "Hello", then what happens? Quote Link to comment https://forums.phpfreaks.com/topic/90555-how-to-compile-e-mail-body-from-assorted-htmlphp-code/#findComment-464495 Share on other sites More sharing options...
njdirt Posted February 12, 2008 Author Share Posted February 12, 2008 If I run the script with the e-mail sending commented out and echo report(); at the end, the proper output is displayed. If I replace the $body to be anything else, it will work without a problem. I'm thinking the method to use would be to do the equivalent of build the html page, have the outputted html (as if you clicked "view source" in a web browser) turned into a single variable like $body. Is this possible? Hmm... Quote Link to comment https://forums.phpfreaks.com/topic/90555-how-to-compile-e-mail-body-from-assorted-htmlphp-code/#findComment-464521 Share on other sites More sharing options...
zimminy Posted February 12, 2008 Share Posted February 12, 2008 This is what I use to email: $headers2="Cc: jody@myhearts.org"; $headers3="Bcc: carmella@hotmail.com"; $headers3.=", sally@hotmail.com"; $headers3=$allleadersemails; $mymessage="Dear Leaders, <ul><BR> Just a note to let all know that we have $numberleaders Hem Leaders available to take e-clients. <BR> If you are a Hem Leader, could you please consider setting your account to accept a Hem client?<BR> There are no clients on a waiting list, so if you do set your account to Accept a Hem client, <BR> you may get a client in a day, or it could even be a week. <BR><BR> Thanks!</ul>"; $from="webservant@myhearts.org"; $fromemail="webservant@myhearts.org"; $mysubject="Leaders needed"; $to="webservant@myhearts.org"; $subject = $mysubject; $message=$mymessage; $headers1= 'MIME-Version: 1.0' . "\r\n"; $headers1.= 'Content-type: text/html; charset=iso-8859-1; Content-type: image/jpg' . "\r\n"; $headers='From: ' . $fromemail . "\r\n"; $headers.=$headers1 . $headers2 . "\r\n"; $headers.=$headers3 . "\r\n"; //Mail it mail($to, $subject, $message, $headers); // this lets it show on the page, so I can see what I sent out echo $mymessage; Quote Link to comment https://forums.phpfreaks.com/topic/90555-how-to-compile-e-mail-body-from-assorted-htmlphp-code/#findComment-464532 Share on other sites More sharing options...
njdirt Posted February 12, 2008 Author Share Posted February 12, 2008 My e-mail function is just about the same as yours, and I know that it works. My problem is creating the body of the email in a way that it can be read and sent through a single variable Quote Link to comment https://forums.phpfreaks.com/topic/90555-how-to-compile-e-mail-body-from-assorted-htmlphp-code/#findComment-465219 Share on other sites More sharing options...
sKunKbad Posted February 12, 2008 Share Posted February 12, 2008 I remember an instance when I was working on a customer's project, and the body of the email would not send until the actual body was flush with the left side of the code. Here is what I found in my code when I looked: //THIS MUST STAY PARKED OVER ON THE LEFT SIDE OF THE CODE *** DO NOT INDENT THIS TO KEEP THE CODE PRETTY OR IT WILL NOT WORK! $message = " --PHP-alt-$random_hash Content-Type: text/plain; charset=\"iso-8859-1\" Content-Transfer-Encoding: 7bit $textEmail --PHP-alt-$random_hash Content-Type: text/html; charset=\"iso-8859-1\" Content-Transfer-Encoding: 7bit $htmlEmail --PHP-alt-$random_hash-- "; // END MESSAGE BODY AREA Quote Link to comment https://forums.phpfreaks.com/topic/90555-how-to-compile-e-mail-body-from-assorted-htmlphp-code/#findComment-465366 Share on other sites More sharing options...
njdirt Posted February 13, 2008 Author Share Posted February 13, 2008 Thanks for the suggestion with the code pushed to the left. I dave it a shot, but as I suspected, it didn't work. In your case, the whole body is a text string within the quotation marks of a variable. The extra spaces and breaks would do weird things to the data. For me, what I want to be included into the e-mail body is a series of lines of code, not plain text. I"ll keep playing around with that to see if it can do me any good though. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/90555-how-to-compile-e-mail-body-from-assorted-htmlphp-code/#findComment-466081 Share on other sites More sharing options...
njdirt Posted February 14, 2008 Author Share Posted February 14, 2008 I got it! I had to create a variable with the html layout, and which is appended to each time more html is generated by the php. A cron tab is set to run the script at night each night to deliver the file. Saweeet!! Thanks for everyone's help <?php $cur_date = date("F d, Y"); include("2/includes/list.php"); $message = "<html><head><title>Repair-Machine</title></head><body><table border=\"0\" width=\"75%\" align=\"center\"><tr><td><h1>Repair Schedule for $cur_date<h1></td></tr><tr><td valign=\"top\" width=\"100%\"><table border=\"1\" width=\"100%\"><tr><td colspan=\"5\"><h2>Todays Repairs</h2></td></tr><tr><td>Tag</td><td>Entry Date</td><td>Make</td><td>Model</td><td>Work Info</td></tr>"; ////////////TODAYS WORK & BUILDS////////////////////////////////////////// //get today's start stamp $begin = time(); $begin_day = mktime(0, 0, 0, date('m', $begin),date('d', $begin),date('Y', $begin)); $query2 = "SELECT * FROM $table4 WHERE est_completion='$begin_day' ORDER BY rid ASC"; $result2 = mysql_db_query ($dbname, $query2, $link); $row_number = "1"; while($row2 = mysql_fetch_array($result2)){ $row_color = ($row_count % 2) ? $color1 : $color2; if ($row2[closed] != "yes"){ $row_color = $color_open; } $bid = $row2[bid]; $cid = $row2[cid]; //see if there is a part on order $query8 = "SELECT status FROM $table5 WHERE cid='$cid' AND rid='$row2[rid]'"; $result8 = mysql_db_query ($dbname, $query8, $link); $row8 = mysql_fetch_array($result8); if ($row8[status] == "0"){ $row_color = $color_order; } if ($row8[status] == "1"){ $row_color = $color_ship; } //get bike info $query3 = "SELECT * FROM $table3 WHERE bid='$bid' LIMIT 1"; $result3 = mysql_db_query ($dbname, $query3, $link); $row3 = mysql_fetch_array($result3); //get ticket id number $query13 = "SELECT tid FROM $table12 WHERE rid='$row2[rid]'"; $result13 = mysql_db_query ($dbname, $query13, $link); $row13 = mysql_fetch_array($result13); if ($row13[tid]==""){ $tid="0"; }else{ $tid=$row13[tid]; } //format date $origin_date = date("F d, Y", $row2[origin_date]); //capitalize first letters in strings $make = ucfirst($row3[brand]); $model = ucfirst($row3[model]); $work = ucfirst($row2[work_needed]); $mes1 = "<tr><td><b>$tid</b></td><td>$origin_date</td><td>$make</td><td>$model</td><td>$work</td></tr>"; //print out the work information $work_order = "<b>Work Order:</b> $row2[work_needed]"; $extra_info = "<b>Extra Information:</b> $row2[work_extra_info]"; $orig = date("D F d, Y", $row2[origin_date]); $origin_date = "<b>Origin Date:</b> $orig"; $sign_in_person = "<b>Sign-In Person:</b> $row2[est_person]"; $mes10 = "<tr><td></td><td colspan=\"4\">$work_order<br>$extra_info<br>$origin_date<br>$sign_in_date</td></tr>"; $row_count++; $row_number++; $message = $message . $mes1 . $mes10; } $mes2 = "<tr><td colspan=\"5\"><h2>Todays Bike Builds</h2></td></tr><tr><td>No.</td><td>Frame Size</td><td>Make</td><td>Model</td><td>Serial Number</td></tr>"; $message = $message . $mes2; //begin displaying build orders in table $query11 = "SELECT sid FROM $table6a WHERE build_date='$begin_day' ORDER BY sid ASC"; $result11 = mysql_db_query ($dbname, $query11, $link); $row_number = "1"; while($row11 = mysql_fetch_array($result11)){ $row_color = $color_build; //get bike info $query12 = "SELECT * FROM $table9 WHERE sid='$row11[sid]' LIMIT 1"; $result12 = mysql_db_query ($dbname, $query12, $link); $row12 = mysql_fetch_array($result12); $mes3 = "<tr><td>$row_number</td><td>$row12[frame_size]</td><td>$row12[make]</td><td>$row12[model]</td><td>$row12[serial]</td></tr>"; $row_number++; $message = $message . $mes3; } $mes4 = "</table><BR><BR><BR></td></tr><tr><td valign=\"top\" width=\"100%\"><table width=\"100%\" border=\"1\"><tr><td colspan=\"5\"><h2>Overdue Repairs</h2></td></tr><tr><td>Tag</td><td>Entry Date</td><td>Make</td><td>Model</td><td>Work Info</td></tr>"; $message = $message . $mes4; //////////////////OVERDUE WORK////////////////////////////////////// //get today's start stamp $begin = time(); $begin_day = mktime(0, 0, 0, date('m', $begin),date('d', $begin),date('Y', $begin)); $query4 = "SELECT * FROM $table4 WHERE est_completion<'$begin_day' AND closed!='yes' ORDER BY rid ASC"; $result4 = mysql_db_query ($dbname, $query4, $link); $row_number = "1"; while($row4 = mysql_fetch_array($result4)){ $row_color = ($row_count % 2) ? $color1 : $color2; $bid = $row4[bid]; $cid = $row4[cid]; //see if there is a part on order $query9 = "SELECT status FROM $table5 WHERE cid='$cid' AND rid='$row4[rid]'"; $result9 = mysql_db_query ($dbname, $query9, $link); $row9 = mysql_fetch_array($result9); if ($row9[status] == "0"){ $row_color = $color_order; } if ($row9[status] == "1"){ $row_color = $color_ship; } //get bike info $query5 = "SELECT * FROM $table3 WHERE bid='$bid' LIMIT 1"; $result5 = mysql_db_query ($dbname, $query5, $link); $row5 = mysql_fetch_array($result5); //get ticket id number $query14 = "SELECT tid FROM $table12 WHERE rid='$row4[rid]'"; $result14 = mysql_db_query ($dbname, $query14, $link); $row14 = mysql_fetch_array($result14); if ($row14[tid]==""){ $tid="0"; }else{ $tid=$row14[tid]; } //format date $origin_date = date("F d, Y", $row4[origin_date]); //capitalize first letters $make = ucfirst($row5[brand]); $model = ucfirst($row5[model]); $work = ucfirst($row4[work_needed]); $mes5 = "<tr><td><b>$tid</b>></td><td>$origin_date</td><td>$make</td><td>$model</td><td>$work</td></tr>"; //print out the work information $work_order = "<b>Work Order:</b> $row5[work_needed]"; $extra_info = "<b>Extra Information:</b> $row5[work_extra_info]"; $orig = date("D F d, Y", $row5[origin_date]); $origin_date = "<b>Origin Date:</b> $orig"; $sign_in_person = "<b>Sign-In Person:</b> $row5[est_person]"; $mes6 = "<tr><td></td><td colspan=\"4\">$work_order<br>$extra_info<br>$origin_date<br>$sign_in_date</td></tr>"; $row_count++; $row_number++; $mes6 = $mes5 . $mes6; $message = $message . $mes6; } $mes7 = "<tr><td colspan=\"5\"><h2>Overdue Bike Builds</h2></td></tr><tr><td>No.</td><td>Frame Size</td><td>Make</td><td>Model</td><td>Serial Number</td></tr>"; $message = $message . $mes7; //begin displaying build orders in table $query11 = "SELECT sid FROM $table6a WHERE build_date<'$begin_day' ORDER BY sid ASC"; $result11 = mysql_db_query ($dbname, $query11, $link); $row_number = "1"; while($row11 = mysql_fetch_array($result11)){ $row_color = $color_build; //get bike info $query12 = "SELECT * FROM $table9 WHERE sid='$row11[sid]' LIMIT 1"; $result12 = mysql_db_query ($dbname, $query12, $link); $row12 = mysql_fetch_array($result12); $mes8 = "<tr><td>$row_number</td><td>$row12[frame_size]</td><td>$row12[make]</td><td >$row12[model]</td><td>$row12[serial]</td></tr>"; $row_number++; $message = $message . $mes8; } $mes9 = "</table></body></html>"; $message = $message . $mes9; //send the actual e-mail $to = "recipient"; $subject = "Repair Machine Schedule for $cur_date"; $body = $message; mail($to, $subject, $message, "To: Bicyle Retailer <shop address>\n" . "From: Repair Machine <server>\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/90555-how-to-compile-e-mail-body-from-assorted-htmlphp-code/#findComment-467139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.