Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending Mail to Many Addresses

Status
Not open for further replies.

dneff

Programmer
Aug 15, 2002
8
US
Hello,

I have a .php page that loops through a database and sends an email to person. There are over 200 emails sent out. I initially had the problem where the scrip would time out (usually because invalid email addresses take more time), so I set set_time_limit
 
try posting the code

Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Here's the meat of the code (excluding the actual database query and some debug code):

function sendMail($fromName, $fromEmail, $toEmail, $subject, $msg, $toName="", $content_type="html")
{
if (!empty($fromName) && !empty($fromEmail) && !empty($toEmail) &&!empty($subject))
{
$from_name = $fromName;
$from_address = $fromEmail;

$to_name = empty($toName) ? $toEmail : $toName;
$to_address = &quot;$toName <$toEmail>&quot;;

$message = $msg;

$headers .= &quot;MIME-Version: 1.0\r\n&quot;;
$headers .= &quot;Content-type: text/&quot; . $content_type . &quot;; charset=iso-8859-1\r\n&quot;;
$headers .= &quot;From: &quot;.$from_name.&quot; <&quot;.$from_address.&quot;>\r\n&quot;;
$headers .= &quot;Reply-To: &quot;.$from_name.&quot; <&quot;.$from_address.&quot;>\r\n&quot;;
$headers .= &quot;X-Priority: 3\r\n&quot;;
$headers .= &quot;X-MSMail-Priority: Low\r\n&quot;;
$headers .= &quot;X-Mailer: iCEx Networks HTML-Mailer v1.0&quot;;

$res = mail($to_address, $subject, $message, $headers);
return $res;
}
return false;
}

..
..
..

while ($line = mysql_fetch_assoc($result))
{
if (!empty($line[&quot;email&quot;]))
{
$toEmail = $line[&quot;email&quot;];
$custID = $line[&quot;custID&quot;];
$name = $line[&quot;name&quot;];
$mailRes = sendMail(&quot;My Title&quot;, &quot;info@MyCompany.com&quot;, $toEmail, $subject, $msg, $name, &quot;html&quot;);
}
}
 
email does take a while to send.

put &quot;set_time_limit(0);&quot; at the top of your code. --BB
 
I'm sorry, I just realized my original message got cut off. I do have ignore_user_abort() and set_time_limit(0) at the top of the code. The problem appears to be that something causes the page to refresh as many as ten times, resulting in everyone getting the same email ten times.

The page itself is the result of a post, but at the end of the post script it does a header so if the client refreshes their browser the posting script isn't run again.

It looks to me like there's still some timeout or retry that's causing the php to automatically reload.

I'm really wondering if there's any additional codeing techniques in php that people are using to programatically deliver many (>200) emails.

Thanks,

-David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top