Page 1 of 1

including sql results in my email? whats going on?!?!?

Posted: Sat Feb 12, 2011 6:01 am
by wright67uk
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?

Code: Select all

<code><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>

Re: including sql results in my email? whats going on?!?!?

Posted: Sat Feb 12, 2011 7:01 am
by divedj
You are pulling the email addresses of your db into the array $email and convert it into a string using your $to variable.
Just try to use $to again in your message. This should display the email addresses.

Code: Select all

$headers = 'From: me@me.com' . "\r\n" .
    'Reply-To: me@me.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$message .= "\r\n". $to ;
echo nl2br ($message);
mail( "$to", "$subject","$message", "$headers");
echo "<br>" . "Thank you for using our mail form.";
?></body></html>

Re: including sql results in my email? whats going on?!?!?

Posted: Sat Feb 12, 2011 8:09 am
by wright67uk
Brilliant that seemed to do the trick.

nl2br, and \r\n dont' seem to work though,
the results in my email are

result1,result2,result2

opposed to

result1
result2
result3

Re: including sql results in my email? whats going on?!?!?

Posted: Sat Feb 12, 2011 9:05 am
by divedj
Great, if you need the email as a column you could try

Code: Select all

foreach($email as $mail)
{
    $message .= $mail."\r\n";
}
Since you are passing your array to $email should the foreach loop pull the single email addresses back out add your linefeed and pass it on to $message.