homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Elegant way of script reloading itself?
mailing list sender throttling

 2:42 am on Nov 22, 2011 (gmt 0)

Ok I'll tell you what I'm trying to do. I'm sick of paying Constant Contact, and Phplist just has too many problems, so I want to make my own e-mail mailer. I'm good on everything except the THROTTLING portion. That is I don't want to dump 5,000 e-mails in one shot. If I slow down the script, even with set_time_limit() set, it could take an hour or more to run the script, so I figure it's better to run say 100 emails, then have the script RELOAD itself? Does this sound doable? Best way to do it?




 7:04 am on Nov 22, 2011 (gmt 0)

two ways I've done this:

1) run the PHP script hosted on Apache and hit via a web browser, and reload the page using client-side javascript.

2) run the PHP script on the command line. Then it doesn't matter that the thing will take over an hour... I have long-running maintenance scripts that take over 12 hours per day, and daemons written in PHP that have been running nonstop for months.

put sleep() into the script to slow it down.


 10:45 am on Nov 22, 2011 (gmt 0)

3) Run the script with CRON every N minutes and send P emails each time (controlled by db flag). Adjust N and P to be within your limits.

I would have thought 5000 emails an hour (83+ a minute) would be very good going if you are able to send that many, without getting blocked for spamming etc.? With a dedicated server you might be OK? For comparison, a shared server I use a lot has a limit of 500 per hour.


 12:57 pm on Nov 22, 2011 (gmt 0)

4) set up Gearman, define sending mail as a "job" and spin up a few workers waiting for "work"... throw your 5,000 emails into the queue and let it run. The number of workers and how quickly they can work will act like a rate limiter - you can force a worker to take a siesta after every job, with sleep()


 4:59 pm on Nov 22, 2011 (gmt 0)

You should **never** attempt this by requesting a script from a page. It will time out, it's almost guaranteed. You can, however, initiate it from your logged in administrator area.

This is not a lighthearted task when you consider all the possibilities - I wouldn't give up on finding something open source that's already made.

Along the lines of penders' solution, your script will set some values in a database for a cron job that runs every [your time period] or so that checks if there is a mailer to be sent, and the record id of the last email sent.

select isactive, email_id, lastrecord from cron_email

(or, some logic based on completed date, see below)

Your entire program is nested in an if

if ((isactive==1) and ($email_id > 0)) {
// do it
when the end of the list is reached, isactive is set to 0. Your email statement for selecting records could be something like


select * from list order by id limit $lastrecord, $mylimit

Where "$mylimit" is the number of emails you send per chunk.

When you're done,

update cron_email set isactive=0,total_sent=$totalsent,completed=now() where email_id=$email_id

Don't do any of this without some sort of logging scheme in place. You need to know what's up when things go sideways (somehow, they always do at some point.)

With a dedicated server you might be OK?

A side note, some dedicated plans (like goDaddy) still run emails through their parent relay and you have to request increases of the limit (the default is 5000 per day.) It's not that they won't allow you, it's just that you have to ask.


 5:24 pm on Nov 22, 2011 (gmt 0)

And it might be worth a mention... from the PHP Manual for the mail() function [uk.php.net]:

It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.

For the sending of large amounts of email, see the PEAR::Mail, and PEAR::Mail_Queue packages.


 5:32 pm on Nov 22, 2011 (gmt 0)

Thanks for the ideas. I like the cron method. We are on a shared server, and I was planning on doing a batch of about 250 an hour. We only send out newsletters about 8 times a year, so it just stinks to pay CC a large monthly fee and sometimes not even use it.

Trust me, I've explored mailing lists endlessly. I've even paid for a few commercial versions. If anyone knows anything out there, please let me know. We don't need anything fancy. Just the basics really. I've installed and uninstalled Phplist probably 5 times over the past 4 years and it disappoints every time.


 9:55 pm on Nov 22, 2011 (gmt 0)

ditto the opinion of Phplist. Tried it, and wore it for a few seconds, until it started to itch in all the wrong places.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved