Jump to content

Mail() function within a while loop


mitydrum

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.