Jump to content

Using sql values as email addresses


wright67uk

Recommended Posts

Im having a little trouble with the script below.  I get my three sql values returned fine, without a problem.

The problem is that Im not sending any emails with it.

 

Wether this is a problem with my syntax, headers, code ??? i dont know.

Please go easy on my scripts, im a learning newbie.

 

<html><body>
<?php
mysql_connect("***","***","***"); 
mysql_select_db("***") 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>";                     
while($row = mysql_fetch_assoc($result)) 
{$message .= "\r\n". $row['email'] ;}
{$value = $row['email'].',';}
rtrim($row, ',');
$i = 0;
{$i++; switch($i)
{case 0:$value1 = $row['email']; break;
case 1:$value2 =$row['email']; break;
case 2:$value3 =$row['email']; break; }}
$to = "$value1, $value2, $value3";
echo nl2br ($message); 
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
mail( "$to", "$subject","$message", "$headers");
echo "<br>" . "Thank you for using our mail form.";
?></body></html>

Link to comment
https://forums.phpfreaks.com/topic/227411-using-sql-values-as-email-addresses/
Share on other sites

I dont understand what is going on here:

while($row = mysql_fetch_assoc($result)) 
{
$message .= "\r\n". $row['email'];
}
{
$value = $row['email'].',';
}
rtrim($row, ',');
$i = 0;
{$i++; switch($i)
{case 0:$value1 = $row['email']; break;
case 1:$value2 =$row['email']; break;
case 2:$value3 =$row['email']; break; }}
$to = "$value1, $value2, $value3";
echo nl2br ($message); 

 

Surely you are getting some errors, no?

I think I'd go about it a little differently. I'm thinking a foreach() loop. This isn't tested, but it should give you some ideas. If you don't need to include the email address in the body of the email, you could just use Bcc: headers to accomplish this.

 

<html><body>
<?php
mysql_connect("***","***","***");
mysql_select_db("***") or die("Unable to select database");

$code = $_GET['postcode'];
$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>";
while( $row = mysql_fetch_assoc($result) ) {
$addresses[] = $row['email'];
}
$error = FALSE;
foreach( $addresses as $val ) {
$message .= "\r\n$val\r\n";
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if( !mail( "$to", "$subject","$message", "$headers") ) {
	$error = TRUE;
}
}
if( $error ) {
echo 'Sorry, there was a problem with the mail form.';
} else {
echo 'Your message has been sent.';
}
?>
</body></html>

[/code]

Thankyou very much! my code now looks like this;

[code=php:0]<html><body>
<?php
mysql_connect("*","*","*"); 
mysql_select_db("*") 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: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\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>

This is great as it send an email to each of the returned sql values.  I have somehow caused my message not to display the returned values though?

I used to click submit on my form and get a list of email addresses ive just emailed.  (this list would appear in my sent email too.) do you know the best way to factor that in?

 

I thought where I put

[syntax=php]{$message .= "\r\n". $row['email'] ;}

echo nl2br ($message);[syntax=php][/syntax]

would display my values for users to see?

 

Many thanks by the way.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.