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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with mail() to multiple recipients

Status
Not open for further replies.

lydhoer

Programmer
Dec 27, 2004
3
DK
Hi,

I'm trying to make an interface for sending newsletters. I use a html form in fileA.php. The form action is fileB.php.

fileB.php is similar to this

<?php
$subject = "Newsletter from Lydhoer.dk";
$header = "From: "robot, lydhoer.dk" <robot@lydhoer.dk>";
$ures = @mysql_query("SELECT * FROM users");
while($urow = mysql_fetch_array($ures))
{
// use stuff from USERS to make mails individuel
$email = $urow['email'];
$message = "nothing";
mail($email, $subject, $message, $header);
}
?>

After loading for a while I get a "500 Internal Server Error" but only when I send to many (approx 70). If I send the newsletter to a smaller group (approx 15) it works out fine! It seems that some of the emails have been sent, but this really of no use...
 
maybe your server blocks that many emails at once, or the script is running a little too fast for the server to handle it. One way might be to add a sleep() command or micro_sleep() to slow the script down a little.

If your server allows this, I'd say the best thing to do would be to have the script call an external (exec) program which actually handles the emailing. That way, the script would set up the variables for the emailing and then return to a page a lot quicker. The user can keep on browsing while the program is running in the background.
 
I bet you are getting a timeout on your server (which you could experiment with increasing, from a default of like 30 seconds to maybe 600 seconds, etc -- do this with set_time_limit(600) or whatever).

Moreover, the lengthy nature of the script probably comes from sending 70 separate emails. In the mail command, you CAN specify multiple email addresses in the "to" field, separating them with ONLY a "," (not ", "). You might want to try sending to like 30 people at a time, instead of 1, for instance. I'd keep the number relatively low, so that you don't accidentally overload the SMTP server or go past some limit they have set.

I would mention that in sending out large volumes of email, where you ARE sending to more than one person at a time (as I suggest above), one "trick" which preserves a little bit of privacy (but may subject you to more "spam" filters) is to put the whole recipient list in the "BCC" header field, and your own email (or some suitable other "reply-to" email) in the actual "to" field. This hides everyone else's email from everyone else, meaning you aren't exposing people's emails to unscrupulous others.

Lastly, I would say, in general, the built in Sendmail (or Postfix or whatever) MTA is not suited for sending out large volumes of email. If you are going to send out more than several hundred emails at a time, you should consider investing in:

1. a dedicated server, with dedicated bandwidth
2. a special MTA that is suited (and tuned) for bulk mailing
 
I have now tried sleep() and set_time_limit(600), but I still get "Internal Server Error". It seems to give me the error message at the same time as before.

I can't really use the BBC-trick, since I need to send individual info (retrieved from SQL DB "users") to each user ... but thanks anyway.

Could I generate these "personalised" newsmails with such an exec you talk about?
 
you're gonna either have to get a special high-volume MTA, or you are gonna have to do manual "socket" programming in PHP and open up a socket to port 25 to sendmail, and then manually send the commands using something like fputs(). This will get complicated, but will probably accomplish what you want and get around the timeouts or errors...

one other thought, could there be some address in your list which gives some sort of syntax error when being sent to mail()?

have you turned on php error reporting (like to E_ALL), and set the error_logging to a log file, and then examined what it prints in that file with one run of the script?
 
The host support guy finally replied. He wrote that they have a block for scripts longer than 30 sec which returns a 500 error. He says that there is no way to reset this.

I'm not that much of programmer so I think I'll have to forget the "socket"-programming.

So --hmmmm-- any advice on sending with external program - how do I do that? I maybe I should go for the BCC-tip...

Thank you so much --- i really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top