wright67uk Posted February 12, 2011 Share Posted February 12, 2011 The below code sends emails, to 3 recipents pulled from my sql database. Each email send successfully. What im trying to do is, display these 3 email addresses within my email message. Thats the bit that isnt working. Any ideas? <html><body> <?php mysql_connect("###,###,###"); mysql_select_db("treesurgery") or die("Unable to select database"); $code = $_GET['postcode']; $message = $_GET['message']; $shortcode = substr($code,0,2); $subject = "subject here"; $result = mysql_query("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3") or die(mysql_error()); echo "<h2>Business Names:</h2>"; $number_of_results = mysql_num_rows($result); $results_counter = 0; if ($number_of_results != 0) {while ($array = mysql_fetch_array($result)) {$email = $array['email']; $results_counter++; if ($results_counter >= $number_of_results) {$to .= $email;} else {$to .= $email . ',';}}} $headers = 'From: me@me.com' . "\r\n" . 'Reply-To: me@me.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); {$message .= "\r\n". $row['email'] ;} echo nl2br ($message); mail( "$to", "$subject","$message", "$headers"); echo "<br>" . "Thank you for using our mail form."; ?></body></html></code> Quote Link to comment https://forums.phpfreaks.com/topic/227449-including-sql-results-within-this-email-whats-going-on/ Share on other sites More sharing options...
denno020 Posted February 13, 2011 Share Posted February 13, 2011 Why don't you change your script so that it sends an email to one person at a time? Which means that when the person receives the email, it'll only have their email address in the to field. This would then make it heaps easy to send the same email to multiple people with specific information (like the person's email address), inside the email... I've been playing with some code and I've got something that will work, so let me know if you want it. Denno Quote Link to comment https://forums.phpfreaks.com/topic/227449-including-sql-results-within-this-email-whats-going-on/#findComment-1173530 Share on other sites More sharing options...
wright67uk Posted February 13, 2011 Author Share Posted February 13, 2011 I got my code to work by changing the later part of it; <html><body><?php mysql_connect("###,###,###"); mysql_select_db("treesurgery") or die("Unable to select database"); $code = $_GET['postcode']; $message = $_GET['message']; $shortcode = substr($code,0,2); $subject = "subject here"; $result = mysql_query("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3") or die(mysql_error()); echo "<h2>Business Names:</h2>"; $number_of_results = mysql_num_rows($result); $results_counter = 0; if ($number_of_results != 0) {while ($array = mysql_fetch_array($result)) {$email = $array['email']; $results_counter++; if ($results_counter >= $number_of_results) {$to .= $email;} else {$to .= $email . ',';}}} $message .= "\r\n". $to ; echo nl2br ($message); echo 'Email addresses: ' . $to . '<br />'; mail( "$to", "$subject","$message"); echo "<br>" . "Thank you for using our mail form."; ?></body></html> The problem I have now is seperating each of the returned values with a new line. changing eg. value1,value2,value3 to value1 value2 value3 also, it would be great to look at your script if the offer's still open. The more I can learn the better! Thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/227449-including-sql-results-within-this-email-whats-going-on/#findComment-1173538 Share on other sites More sharing options...
denno020 Posted February 13, 2011 Share Posted February 13, 2011 $code = $_GET['postcode']; $message = $_GET['message']; $shortcode = substr($code,0,2); $subject = "subject here"; $headers = 'From: me@me.com' . "\r\n" . 'Reply-To: me@me.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $result = mysql_query("SELECT email FROM treesurgeons WHERE postcode like '%" . $shortcode . "%' ORDER BY companyName LIMIT 3") or die (mysql_error()); while ($array = mysql_fetch_array($result)) { $email = $array['email']; $to = $email; $message .= 'Message is being sent to: ' . $email; echo nl2br ($message); mail( "$to", "$subject","$message", "$headers"); } echo "<h2>Business Names:</h2>"; echo "<br>" . "Thank you for using our mail form."; That's the sort of thing I would do. So the while loop that goes through the result set from the query will send the mail out. This way there will only be 1 email address in the 'To' field, and the address it's being sent to can easily be added to the message. What are you trying to achieve though? Where should the 'one on each line' email addresses be displayed? Should this be on the confirmation page or should this be in each email itself? If it's the former, then you could simply add each email address to a new variable inside the while loop as such: //before the while loop $sentTo = ''; //inside the while loop $sentTo .= $email . '<br/>'; //at the place you want it to be displayed on the confirmation page echo $sentTo; Hope that helps . Denno Quote Link to comment https://forums.phpfreaks.com/topic/227449-including-sql-results-within-this-email-whats-going-on/#findComment-1173542 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.