Jump to content

including sql results within this email? whats going on !?!?


Recommended Posts

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>

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

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.

$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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.