Programmatically speaking, what would b开发者_运维知识库e a simple and straightforward way of spacing out the sending of bulk email while avoiding a PHP timeout? This is for contacting a few thousand members belonging to a site.
P.S: Thinking along the terms of splitting up sends into N numbers of email addresses and having a script somehow call itself.
Easiest way would be just to sleep for some seconds after every x number of emails sent:
$count = 0
while (foo) {
send_email();
if ($count++ == 100) {
sleep(10);//sleep for 10 seconds
$count = 0;
}
}
If you are sending to everyone subscribed to the site, you could do the following:
- Add a column (if it doesn't already exist) on the user table, something like 'email_sent' and default to 1 (for yes)
- When you execute your email send trigger, update all user records setting the 'email_sent' flag to 0 (for no).
- Set up a cron job that executes a PHP script (or even hits your web server using a designated page to execute the script) that then selects the first N users that have 'email_sent' set to 0, send them emails, and update the 'email_sent' column to 1 for each that succeeds.
If you're handling multiple mailings, you would need to join across another table that maintains the user:mailout relationship and 'email_sent' status.
You can always try the sleep command or manually staggered cronjobs but a better option may be looking into an established library that handles details for you: PEAR Mail_Queue
The Mail_Queue class puts mails in a temporary container, waiting to be fed to the MTA (Mail Transport Agent), and sends them later (e.g. a certain amount of mails every few minutes) by crontab or in other way.
There are also many companies that will handle all of this for you at a price, if that's an option for you.
精彩评论