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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wright67uk
Forum Newbie
Posts: 7
Joined: Fri Jan 01, 2010 4:09 pm

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

Post 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>
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

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

Post 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>
wright67uk
Forum Newbie
Posts: 7
Joined: Fri Jan 01, 2010 4:09 pm

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

Post 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
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

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

Post 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.
Post Reply