wright67uk Posted February 11, 2011 Share Posted February 11, 2011 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: me@me.com' . "\r\n" . 'Reply-To: me@me.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail( "$to", "$subject","$message", "$headers"); echo "<br>" . "Thank you for using our mail form."; ?></body></html> Quote Link to comment Share on other sites More sharing options...
rondog Posted February 11, 2011 Share Posted February 11, 2011 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? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 11, 2011 Share Posted February 11, 2011 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: me@me.com' . "\r\n" . 'Reply-To: me@me.com' . "\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] Quote Link to comment Share on other sites More sharing options...
wright67uk Posted February 12, 2011 Author Share Posted February 12, 2011 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: 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> 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. Quote Link to comment 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.