Jump to content

Sending E-MAILS After fetching Multiple records


Ashish Sood

Recommended Posts

I want to sending a mail after fetching multiple records from the table, the searching is based on date, mean what ever the records entered by user on the current date that need to be mail to the specific person.

 

table from which is need to fetch the records is order_details

 

below is my current script..

$selectcondition = "SELECT * FROM order_details where DATE='".$date."'"; // From this table i need to fetch all the records date wise.                        $check=mysql_query($selectcondition,$con);                        $fetch=mysql_fetch_row($check);                                                                        while($row = mysql_fetch_array($fetch))                        {                          //                        }                                                    $from="REPORT@abc.com";                        $to="admin@abc.com";                        $subject="Customer report of $date";                                                                        $message ="<html>                                   <body>                                   <hr>                                                                      <p> <strong><font color ='#69571F'><em>First</em></font></strong> =><font color ='#858685'> {$first}</font></p>                                   <p> <strong><font color ='#69571F'><em>Last Person</em></font></strong> =><font color ='#858685'> {$last}</font></p>                                   <p> <strong><font color ='#69571F'><em>Comments </em></font></strong> =><font color ='#858685'> {$comments}</font></p>                                   <hr>                                   </body>                                   </html>";                                                           $header = 'MIME-Version: 1.0' . "rn";                        $header .= 'Content-type: text/html; charset=iso-8859-1' . "rn";                                    $header .= "From: {$from}rn";                        $header .= "Reply-To: {$to}rn";                        $header .= "Return-path: " . $to;                                                            //mail($to, $subject, $message,"From:".$from, "-f$from");                                                mail( $to,  $subject, $message, $header, "-f{$from}");            }   

Right now only the customer information are sending with mail, now i want customer orders detail also should be part of body of a mail... ( single mail is needed for both the thing customer information & order details)

 

Hope i can able to understand you guys my need , Please let me know if something you need to know more on this..

 

Thanks In Advance..

 

 

Link to comment
Share on other sites

Build your message string to have what you want the start of the email message to be, then add your while loop to loop through all of the records and add whatever data you want to the message body, then have the text at the end of the message.

Link to comment
Share on other sites

Getting an error

 

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:inetpubwwwrootemailcustomer.php on line 141.

 

while ($records = mysql_fetch_array($fetch)) { $message .= 'record for user ' . $records['TIMESTAMPS'] . '<br>'; // LINE NO 141 $message .= 'record for user ' . $records['CUSTOMERID'] . '<br>'; $message .= 'record for user ' . $records['ORDER'] . '<br>'; $message .= 'record for user ' . $records['COMMENTS'] . '<br>'; }

Link to comment
Share on other sites

Yes you are right , how could i overcome from it ? Below is my complete code

<?phpif(isset($_POST['submit_shift'])){			include("../../../../../../../inetpub/wwwroot/dbcon/connection.php");			$selectdb=mysql_select_db("customer",$con);   			if(!$selectdb)			{				die ("Database Connection Error" .mysql_error());			}						$outgoing= htmlspecialchars($_POST['outgoing']);			$incoming= htmlspecialchars($_POST['incoming']);	 		$comments= htmlspecialchars($_POST['shift_comments']);						if (!preg_match('/^[a-zA-Z0-9ds.]*$/', $outgoing . $incoming . $comments))			{				$turnmsg = "<b><font color='red'> Only letters,spaces,decimal & .  are allowed </font></b>";			}						else			{							$sql = mysql_query("INSERT INTO customer (DATE,FIRST,LAST,COMMENTS) VALUES('$date','$FIRST','$LAST','$comments')")				or die ("Database Connection Error" .mysql_error());						$turnmsg = "<br><b> <font color='green'>THANKS FOR REGISTRATION</b></font>";								        															    $selectcondition = "SELECT * FROM order_detail where DATE='".$date."'";							$check=mysql_query($selectcondition,$con);						$fetch=mysql_fetch_row($check);																																								$from="REPORT@abc.com";                        $to="admin@abc.com";                        $subject="Customer report of $date";	            														$message ="<html>								   <body>								   <hr>								   								   <p> <strong><font color ='#69571F'><em>First</em></font></strong> =><font color ='#858685'> {$first}</font></p>								   <p> <strong><font color ='#69571F'><em>Last</em></font></strong> =><font color ='#858685'> {$last}</font></p>								   <p> <strong><font color ='#69571F'><em>Comments </em></font></strong> =><font color ='#858685'> {$comments}</font></p>								   								   <p> ORDER Details </p>								   while($records = mysql_fetch_array($fetch))								   {																				$message .= 'record for user ' . $records['TIMESTAMPS'] . '<br>';										$message .= 'record for user ' . $records['ORDERID'] . '<br>';										$message .= 'record for user ' . $records['ORDERNUM'] . '<br>';										$message .= 'record for user ' . $records['STATUS'] . '<br>';									    $message .= 'record for user ' . $records['COMMENTS'] . '<br>';									}																											   								   <hr>								   </body>								   </html>";								   						$header = 'MIME-Version: 1.0' . "rn";						$header .= 'Content-type: text/html; charset=iso-8859-1' . "rn";									$header .= "From: {$from}rn"; 						$header .= "Reply-To: {$to}rn"; 						$header .= "Return-path: " . $to; 		   												//mail($to, $subject, $message,"From:".$from, "-f$from");						 						mail( $to,  $subject, $message, $header, "-f{$from}"); 			}	?>
Link to comment
Share on other sites

Still having the same error

                                                                   $message = 'Order Details';								  while($records = mysql_fetch_array($fetch))                                                                  {                                                                                                               $message .= 'record for user ' . $records['TIMESTAMPS'] . '<br>';                                                                       $message .= 'record for user ' . $records['ORDERID'] . '<br>';                                                                       $message .= 'record for user ' . $records['ORDERNUM'] . '<br>';                                                                       $message .= 'record for user ' . $records['STATUS'] . '<br>';                                                                       $message .= 'record for user ' . $records['COMMENTS'] . '<br>';                                                                   }																		$message .= 'END';
Edited by Ashish Sood
Link to comment
Share on other sites

The code you posted in post 9 does not have a syntax error. Did you also put that code in the middle of a string without closing the string properly? I doubt the problem with your code is the loop, it's probably how you're dealing with the email body text.

Link to comment
Share on other sites

<?phpif(isset($_POST['submit_shift'])){			include("../../../../../../../inetpub/wwwroot/dbcon/connection.php");			$selectdb=mysql_select_db("customer",$con);   			if(!$selectdb)			{				die ("Database Connection Error" .mysql_error());			}						$outgoing= htmlspecialchars($_POST['outgoing']);			$incoming= htmlspecialchars($_POST['incoming']);	 		$comments= htmlspecialchars($_POST['shift_comments']);						if (!preg_match('/^[a-zA-Z0-9ds.]*$/', $outgoing . $incoming . $comments))			{				$turnmsg = "<b><font color='red'> Only letters,spaces,decimal & .  are allowed </font></b>";			}						else			{							$sql = mysql_query("INSERT INTO customer (DATE,FIRST,LAST,COMMENTS) VALUES('$date','$FIRST','$LAST','$comments')")				or die ("Database Connection Error" .mysql_error());						$turnmsg = "<br><b> <font color='green'>THANKS FOR REGISTRATION</b></font>";								        															    $selectcondition = "SELECT * FROM order_detail where DATE='".$date."'";							$check=mysql_query($selectcondition,$con);						$fetch=mysql_fetch_row($check);																									$from="REPORT@abc.com";                        $to="admin@abc.com";                        $subject="Customer report of $date";	            														$message ="<html>								   <body>								   <hr>								   								   <p> <strong><font color ='#69571F'><em>First</em></font></strong> =><font color ='#858685'> {$first}</font></p>								   <p> <strong><font color ='#69571F'><em>Last</em></font></strong> =><font color ='#858685'> {$last}</font></p>								   <p> <strong><font color ='#69571F'><em>Comments </em></font></strong> =><font color ='#858685'> {$comments}</font></p>								   								    $message = 'Order Details';								    while($records = mysql_fetch_array($fetch))                                    {                                                                                $message .= 'record for user ' . $records['TIMESTAMPS'] . '<br>';                                        $message .= 'record for user ' . $records['ORDERID'] . '<br>';                                        $message .= 'record for user ' . $records['ORDERNUM'] . '<br>';                                        $message .= 'record for user ' . $records['STATUS'] . '<br>';                                        $message .= 'record for user ' . $records['COMMENTS'] . '<br>';                                    									}																		$message .= 'END';																		   								   <hr>								   </body>								   </html>";								   						$header = 'MIME-Version: 1.0' . "rn";						$header .= 'Content-type: text/html; charset=iso-8859-1' . "rn";									$header .= "From: {$from}rn"; 						$header .= "Reply-To: {$to}rn"; 						$header .= "Return-path: " . $to; 		   												//mail($to, $subject, $message,"From:".$from, "-f$from");						 						mail( $to,  $subject, $message, $header, "-f{$from}"); 			}	?>
Link to comment
Share on other sites

So, look at that code. Look at the body text for the email, see how all of that code is green? That's because all of the code is inside of a PHP string. It is not PHP code, it is text that you are putting inside of a string, inside of the email text. So you're saying that you want the email text to contain "while($records = ...". Like I mentioned before, when you build the email text you need to do it in three parts. Right now you only have one part, which is all of the text plus some PHP code mixed in with the text. The PHP code can't be inside the text. It is not text, it is code to execute. The first part of the text needs to be the start of the email, from the <html> part down to just before where you want the records to print. That's the start. After that, you end the text string. Then you put the loop there, to loop through the records and add each one to the text. After the loop, you add the end of the text, whatever HTML code you want to come after the records. That's the same as I showed in post 4, where it has a start, then the loop, then the end. That's what this is:

$message = 'start of email text';while ($row = mysql_fetch_array($fetch)) {  $message .= 'record for user ' . $row['user'] . '<br>';}$message .= 'end of email text';

If I had that like you have your code, it would look like this:

$message = "start of email textwhile ($row = mysql_fetch_array($fetch)) {  $message .= 'record for user ' . $row['user'] . '<br>';}end of email text";

See what the problem is? Notice the difference in coloring in the two pieces of code. One of them is all green, because it's all text.

Link to comment
Share on other sites

Thanks, I understood the problem , i have made the changes into my code after that i able to overcome from the problem, But still order details will not come along with that mail ? simply customer details are coming along with the mail .

<?phpif(isset($_POST['submit_shift'])){			include("../../../../../../../inetpub/wwwroot/dbcon/connection.php");			$selectdb=mysql_select_db("customer",$con);   			if(!$selectdb)			{				die ("Database Connection Error" .mysql_error());			}						$outgoing= htmlspecialchars($_POST['outgoing']);			$incoming= htmlspecialchars($_POST['incoming']);	 		$comments= htmlspecialchars($_POST['shift_comments']);						if (!preg_match('/^[a-zA-Z0-9ds.]*$/', $outgoing . $incoming . $comments))			{				$turnmsg = "<b><font color='red'> Only letters,spaces,decimal & .  are allowed </font></b>";			}						else			{							$sql = mysql_query("INSERT INTO customer (DATE,FIRST,LAST,COMMENTS) VALUES('$date','$FIRST','$LAST','$comments')")				or die ("Database Connection Error" .mysql_error());						$turnmsg = "<br><b> <font color='green'>THANKS FOR REGISTRATION</b></font>";								        															       $selectcondition = "SELECT * FROM order_detail where DATE='".$date."'";							$check=mysql_query($selectcondition,$con);						$fetch=mysql_fetch_row($check);																									$from="REPORT@abc.com";                                                $to="admin@abc.com";                                                $subject="Customer report of $date";                                                                                                                                      $message = 'Order Details';								    while($records = mysql_fetch_array($fetch))                                                                     {                                                                                                                           $message .= 'record for user ' . $records['TIMESTAMPS'] . '<br>';                                                                                   $message .= 'record for user ' . $records['ORDERID'] . '<br>';                                                                                   $message .= 'record for user ' . $records['ORDERNUM'] . '<br>';                                                                                   $message .= 'record for user ' . $records['STATUS'] . '<br>';                                                                                   $message .= 'record for user ' . $records['COMMENTS'] . '<br>';                                    									}																		$message .= 'END';	            														$message ="<html>								   <body>								   <hr>								   								   <p> <strong><font color ='#69571F'><em>First</em></font></strong> =><font color ='#858685'> {$first}</font></p>								   <p> <strong><font color ='#69571F'><em>Last</em></font></strong> =><font color ='#858685'> {$last}</font></p>								   <p> <strong><font color ='#69571F'><em>Comments </em></font></strong> =><font color ='#858685'> {$comments}</font></p>								   								 																		   								   <hr>								   </body>								   </html>";								   						$header = 'MIME-Version: 1.0' . "rn";						$header .= 'Content-type: text/html; charset=iso-8859-1' . "rn";									$header .= "From: {$from}rn"; 						$header .= "Reply-To: {$to}rn"; 						$header .= "Return-path: " . $to; 		   												//mail($to, $subject, $message,"From:".$from, "-f$from");						 						mail( $to,  $subject, $message, $header, "-f{$from}"); 			}	?>
Edited by Ashish Sood
Link to comment
Share on other sites

Sorry for replying late, i was out of town,

I have changed the variable message => order , But still no order detail coming with mail, Except only the "order description" text

$order = Order DESCRIPTION';								   while($records = mysql_fetch_array($check))								    {																					$order .= 'record for user ' . $records['TIMESTAMPS'] . '<br>';											$order .= 'record for user ' . $records['ORDERID'] . '<br>';											$order .= 'record for user ' . $records['ORDERNUM'] . '<br>';											$order .= 'record for user ' . $records['SATUS'] . '<br>';									    	$order .= 'record for user ' . $records['COMMENTS'] . '<br>';									 }$order.= 'END';	            								$message ="<html><body><hr>								   <p> <strong><font color ='#69571F'><em>First</em></font></strong> =><font color ='#858685'> {$first}</font></p><p> <strong><font color ='#69571F'><em>Last</em></font></strong> =><font color ='#858685'> {$last}</font></p><p> <strong><font color ='#69571F'><em>Comments </em></font></strong> =><font color ='#858685'> {$comments}</font></p>								   								 																		   <hr></body></html>";								   $header = 'MIME-Version: 1.0' . "rn";$header .= 'Content-type: text/html; charset=iso-8859-1' . "rn";			$header .= "From: {$from}rn"; $header .= "Reply-To: {$to}rn"; $header .= "Return-path: " . $to; 		   												 mail( $to,  $subject, $message. $order, $header, "-f{$from}"); }	
Edited by Ashish Sood
Link to comment
Share on other sites

Unable to format the data which is inside of loop, Any help

while($records = mysql_fetch_array($check))								    {																					$order .= 'record for user ' . $records['TIMESTAMPS'] . '<br>';											$order .= 'record for user ' . $records['ORDERID'] . '<br>';											$order .= 'record for user ' . $records['ORDERNUM'] . '<br>';											$order .= 'record for user ' . $records['SATUS'] . '<br>';									    	$order .= 'record for user ' . $records['COMMENTS'] . '<br>';									 }

Please Reply me ASAP

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...