mitydrum Posted January 24, 2008 Share Posted January 24, 2008 I am new to this so please forgive me if I don't provide all the information needed. I've got a simple email message form. I type in the message I want to send out, then query the database for the email addresses in the table, then send the message to each distinct email address individually. This has worked flawlessly up until the beginning of the year. I'm using an independent web host service and the email services in the php.ini file appear to be set up properly. Here is the code once the message has been typed and submitted... <?php include('data.inc'); include('connect.inc'); $submit = $_POST['submit']; $message = $_POST['message']; if(isset($submit)){ $subject = "Newsletter"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "To: Recipient \r\n"; $headers .= "From: Administrator <admin@whereever.com> \r\n"; $newmessage = stripslashes($message); $newmessages = nl2br($newmessage); $find = "SELECT DISTINCT email FROM <table> ORDER BY email ASC"; $get = mysql_query($find) or die(mysql_error()); $list = array(); $i = 0; while($have = mysql_fetch_array($get)){ $list[$i] = $have[0]; $i++; } foreach($list as $emailvalue){ $email="$emailvalue"; mail($email, subject, $newmessages, $headers); } header("LOCATION:adminemail.php"); } ?> What's happening is that the query will cycle through once then 'die' or timeout - I'll get the one email sent. The web page will display a "this web page can not be found..." message as if it is like it is losing connection to the server. The include functions are the same that I use to pull data for other pages and they work without error. Am I missing something or is there a better way to do this. Please help...I'll take any advice given and try it. Thanks in advance. -Frustrated Quote Link to comment https://forums.phpfreaks.com/topic/87647-mail-function-within-a-while-loop/ Share on other sites More sharing options...
kenrbnsn Posted January 24, 2008 Share Posted January 24, 2008 I would remove the header() function until you figure out what's wrong. Put a <?php echo '<pre>' . print_r($list,true) . '</pre>'; ?> before the foreach() loop to see what's in the array. Ken Quote Link to comment https://forums.phpfreaks.com/topic/87647-mail-function-within-a-while-loop/#findComment-448289 Share on other sites More sharing options...
mitydrum Posted January 24, 2008 Author Share Posted January 24, 2008 Ken Thanks for replying so quickly! I tried inputing the print_r() function before the foreach loop and commented out the header() function. The exact same thing happened where the page would display "This page can not be found...". I tried commenting out the mail() function and placed an exit() function in place of the header() function and the page displayed nothing or did not print out the array. I tried this to see if the $list array was empty or not. print_r($list, true); $num = count($list); echo $num; foreach($list as $emailvalue){ $email="$emailvalue"; //mail($email, subject, $newmessages, $headers); } exit(); The result showed exactly how many email address were listed in the database table? Why would it now print the items in the array if it is NOT empty? Quote Link to comment https://forums.phpfreaks.com/topic/87647-mail-function-within-a-while-loop/#findComment-448319 Share on other sites More sharing options...
mitydrum Posted January 24, 2008 Author Share Posted January 24, 2008 Ken I tried the print_r() without the 'true' boolean and it worked fine and displayed everything that should be in the array. I also tried the var_dump() function and it worked just fine as well. So the data is there.... It all appears to be stemming from the mail() function. I tried the following <?php mail($email, $subject, $newmessages, $headers) or die(mysql_error()); ?> ...and did not receive any errors. I'm not sure if you can use this function here but I thought I'd give it a try. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/87647-mail-function-within-a-while-loop/#findComment-448334 Share on other sites More sharing options...
kenrbnsn Posted January 25, 2008 Share Posted January 25, 2008 You forgot the "$" on the "subject" <?php mail($email, $subject, $newmessages, $headers); // added $ ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/87647-mail-function-within-a-while-loop/#findComment-448437 Share on other sites More sharing options...
mitydrum Posted January 28, 2008 Author Share Posted January 28, 2008 Ken, Sorry for the long delay. I was out of the office this weekend. I have since added the '$' to subject with the same results. Tony Quote Link to comment https://forums.phpfreaks.com/topic/87647-mail-function-within-a-while-loop/#findComment-451316 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.