views:

210

answers:

5

I have to made a page which will send Email to Newsletter subscribers. There is more then 14000 subscriber. I want to use php mail() function to send Email to them. But I'm afraid that it will not be able to send email to all subscribers for php 30sec max_execution_time limit. Its not possible to test how much Email can be sent by sending Test Email to subscribers. So I want to know how much Email can be sent with mail() function in 30 second max_execution_time limit. Will be very helpful if you can answer me.

also another question - Is mysql execution time is also count in php?

Apache version 2.2.13 (Unix) PHP version 5.2.11

+4  A: 

The php max_execution_time setting is customizable. 30 seconds is the default but you can set it to 0 seconds for no execution time limit at all. Use set_time_limit().

set_time_limit(0);

If you do this, you should be able to send all your email.

Please be careful about sending more than one email to the same mail server per second. You don't want to get blacklisted.

Asaph
Don't forget that some (most?) mail server will throttle incoming connections to avoid spam and other nastyness. Better is to send one mail say every second, and definitely use a cronjob/background task with an increased max_execution_time.
Wim
@Wim: Good point. There are other approaches to deal with this. For example you could also make sure to not send email to any one server twice in the same second using a round robin strategy. That would allow you to have high throughput while not hammering any single mail server.
Asaph
@Wim: I updated my answer to mention throttling.
Asaph
+3  A: 

You should run this from a cronjob or spawn a background task or use something else better suited to batch jobs.

You might get 14000 emails out in 30sec if your mailserver is fast enough, but what happens when you get a few more subscribers and it stops working properly?

Perhaps you can set a flag in the database for each user, then reset the flag as their email is sent by a background task. That will help to avoid duplicates and so on if there is a problem with the mail server.

gnibbler
+2  A: 

That depends on so many variables that a single answer is not possible. Factors include:

  1. The speed of the CPU
  2. The bandwidth available from the sending system to the MTA
  3. The MTA's capacity to accept emails

The only way to find out is to try it.

Jim Garrison
A: 

Seriously, if you want to send the same mail to ten people from your normal mailapp, do you normally create ten identical mails or do you just send the mail once adding the recipients to the send list?

Edit: If the answer is "I send it once", I think you should look in that direction here as well (it is even described how to send to multiple recipients at http://www.php.net/mail)

Fredrik
If you want customized content ("Hello <Name>,"), or a direct unsubscribe link, then yes I'll send 10 different e-mails.Which is exactly the reason why I'll use a script instead of my desktop mailapp.
Wim
Unfortunately the MTA will silently truncate the send to list if it is too long. Also subscriber emails are often personalised with the subscriber's name
gnibbler
@Wim: True, but in that case you are trying to use a tool in a way it is not intended to. There are better ways then calling mail() from a script in such cases.@gnibbler: Of course, but having 20 recipients per mail is going to cut the number of smtp-chats a lot.
Fredrik
You could BCC all people with the same first name ;) `Dear Bob,`
sirlancelot
A: 

I had this exact problem a while back on one of my projects. The solution is to isolate the email sending from the actual site.

I coded a small class that would be called to send an email. It would be passed a templated email, which it would then store into the database in a mail queue. On the back end, I had a cron job that called a mailer script every X seconds. Script looks at the database queue for emails, grabs X number from the queue to attempt to send (ordered by timestamp in), then would attempt delivery. Assuming no errors were thrown, script would mark the message as sent. Next step would be to purge all emails from the queue that were sent and older than X days (kept for logging).

Hope that is helpful.

Ben Dauphinee