Bertholt Posted June 29, 2007 Share Posted June 29, 2007 Hello, It is probably a simple question but I am new to php... How can i insert the results of a while loop into an email? How can i store the results of the loop and later add this into an email send with php? I added the loop below. Thanx in advance.... <?php $sql = "SELECT * FROM machine WHERE DATE_SUB(NOW(),INTERVAL 7 DAY) <= `update_time`"; $result1 = mysql_query($sql); $EnDecryptText = new EnDecryptText(); while($row = mysql_fetch_assoc($result1)) { echo $row['name'].'<br>'; $machine_encrypt = $EnDecryptText->Encrypt_Text($row['machine_id']); echo'http://'.$_SERVER['HTTP_HOST'].'/machine_new.php?'.$machine_encrypt.'<br><br><br>'; } ?> Quote Link to comment Share on other sites More sharing options...
phpknight Posted June 29, 2007 Share Posted June 29, 2007 Create a blank string outside the while loop ($msg=""; ). Then, keep filling it with .= for whatever you want. Send that as your email message after you get out of the loop. mail ($email, $sub, $msg); Quote Link to comment Share on other sites More sharing options...
Bertholt Posted June 29, 2007 Author Share Posted June 29, 2007 Create a blank string outside the while loop ($msg=""; ). Then, keep filling it with .= for whatever you want. Send that as your email message after you get out of the loop. mail ($email, $sub, $msg); I have tried to create the file but it still wont work: Here is my code... if (mysql_num_rows($result1) > 0) { while ($email_arr = mysql_fetch_array ($result2)) { $to = $email_arr['email']; $from = "info@test.com"; $subject = "Registration"; $msg="Dear ".$email_arr['firstname']." ".$email_arr['lastname']."\n\n"; $msg.="We have added some new items that might interest you.\n\n"; $msg.="Click on the following link to visit items :\n"; $msg1=""; while($row = mysql_fetch_assoc($result1)) { $msg1.= $row['name'].'<br>'; $machine_encrypt = $EnDecryptText->Encrypt_Text($row['machine_id']); $msg1.= 'http://'.$_SERVER['HTTP_HOST'].'/machine_new.php?'.$machine_encrypt.'<br><br><br>'; $mailsend = mail($to, $subject, $msg1, "From: $from"); $send_info_email_arr .= "\n".$to."\n"; } any ideas? Quote Link to comment Share on other sites More sharing options...
phpknight Posted June 29, 2007 Share Posted June 29, 2007 When you say, "it won't work," what do you mean? What do you get in the email (or not), and what would you like to see exactly? BTW, your $msg is not getting sent, only $msg1. You are also missing a semi-colon on that second line in the loop. Quote Link to comment Share on other sites More sharing options...
Bertholt Posted June 29, 2007 Author Share Posted June 29, 2007 When you say, "it won't work," what do you mean? What do you get in the email (or not), and what would you like to see exactly? BTW, your $msg is not getting sent, only $msg1. You are also missing a semi-colon on that second line in the loop. Well, i created msg1 just for a test and i get the mail but the data from the while loop aren't in it .. It is just an empty mail. Quote Link to comment Share on other sites More sharing options...
suma237 Posted June 29, 2007 Share Posted June 29, 2007 keep the mail function out the while loop and also check the variable $msg1 Quote Link to comment Share on other sites More sharing options...
phpknight Posted June 29, 2007 Share Posted June 29, 2007 Also, write a print test statement to make sure it is even entering the while at all. It might not be. Quote Link to comment Share on other sites More sharing options...
Bertholt Posted June 29, 2007 Author Share Posted June 29, 2007 Thanks for the quick replies. Underneath I add the whole php code. I am sure the loop works because i tested him in an new empty document. I get the mails send to the $to variable. I also get the $msg text (Dear NAME etc. ) i just dont get the data from the while loop (see BOLD text) <?php // Connection database + encrypt script require_once('../Connections/conn.php'); include('../lib/endecrypt.php'); mysql_select_db($database_conn, $conn); // Get machines of last week $sql = "SELECT * FROM machine WHERE DATE_SUB(NOW(),INTERVAL 7 DAY) <= `update_time`"; $result1 = mysql_query($sql); $EnDecryptText = new EnDecryptText(); // Get the data from the users $selectSQL = "SELECT * FROM user WHERE keepinformed = 1"; if ($rowMachine['status_id'] != 5) { // If not 'for sale' only select the valid S&N-users $selectSQL .= " AND valid = 1"; } $result2 = mysql_query($selectSQL); // Send Mail if (mysql_num_rows($result1) > 0) { while ($email_arr = mysql_fetch_array ($result2)) { //$to = $email_arr['email']; $to = "test@test.be"; $from = "info@test.com"; $subject = "Registration test.com"; $msg="Dear ".$email_arr['firstname']." ".$email_arr['lastname']."\n\n"; $msg.="We have added some new machines at Secondhandbev.com that might interest you.\n\n"; $msg.="Click on the following link to visit the machine sheets :\n"; while($row = mysql_fetch_assoc($result1)) { $msg.= $row['name'].'<br>'; $machine_encrypt = $EnDecryptText->Encrypt_Text($row['machine_id']); $msg.= 'http://'.$_SERVER['HTTP_HOST'].'/machine_new.php?'.$machine_encrypt.'<br><br><br>'; } $mailsend = mail($to, $subject, $msg1, "From: $from"); $send_info_email_arr .= "\n".$to."\n"; } // Send delivery mail $info_msg.= "I sent mail to the following people: <BR><BR>\n"; $info_msg.= $send_info_email_arr; $info_mail = mail('test@test.com', "Cron job", $info_msg, "From: webmaster@test.com"); } else{ $info_msg = " no results this week"; $info_mail = mail('test@test.com',"Cron job voor machines", $info_msg, "From: webmaster@test.com"); } ?> Quote Link to comment Share on other sites More sharing options...
Bertholt Posted June 29, 2007 Author Share Posted June 29, 2007 Nobody to the rescue? It's probably a stupid error that i made but i can't find it... Quote Link to comment 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.