tags:

views:

985

answers:

6

What is the most proper way to sending email of minimal 1000 or more in php? Any reliable email queueing technique that is capable to handle that?

+2  A: 

You could just insert your emails into a Mail Queue database table, and have a separate process check the queue and batch send a certain number at once.

mercutio
A: 

I've generally relied on a hack. I have a database list of email addresses and then use a meta-redirect to self with an increasing 'offset' parameter that specifies which row in the database I am up to. Server redirects cause problems because browsers assume that the time taken indicates an infinite loop.

Graphain
A: 

as mercutio suggested, i would insert a new record into a mail queue table for each email waiting to be sent and then use a separate process (like a CRON) to check the table periodically for any queued items.

if any emails are queued (and the email is not customised for each recipient) i would then group the emails by domain and send blocks together to reduce the total number of emails that have to be sent, i.e. if you have 1000 emails queued and 250 are to gmail accounts i would send the 250 in 25 blocks of 10 (remember to Bcc recipients to avoid them seeing each other).

to actually send the mail i would use PEAR mail over php's mail() function

after sending the email either delete record(s) from the queue or change a status flag to show it was sent and loop - i would also add a counter to keep track of emails that failed to send and remove them after x failed attempts

to overcome timeout issues i would either,(depending on the situation) - set the settimelimit() to x seconds and keep track of the script execution time (killing the script after (x-1) seconds) - call the script from the command line to avoid timeouts - set a limit to the number of emails the script could send in one execution

JimmyJ
+1  A: 

Sure, the database table might be an idea. But what about sending 1000 e-mails with a 2mb attachment? you'd have to take that into account as well. I had the problem myself, and I eventually resorted to writing the e-mail to the database, and the files to the filesystem. The e-mail script I used then read the database records, and tried to fetch the attachments to send.

Erik van Brakel
+2  A: 

There's a tested solution for that: PEAR Mail_Queue

Works fine for me.

BlaM
I've used this to queue up 30,000+ mails at a time. A fairly simple script sends around 250 a time - if the load average isn't too high. You won't send them any faster, but it will do it without any problems.
Alister Bulman
+1  A: 

Are you sure you need do this mail queuing yourself?

Just deliver all mail to the local machine's mail transfer agent (sendmail...) and let that take care of the queuing and sending. After all, that's what was designed for.

In other words: don't worry about it!

rix0rrr
if you are delivering a couple of dozen, that's not a problem. Sendign thousands of emails will spin up a new sendmail process for everyone - and then you end up with large load-averages that bring the server to it's knees. I've Pear_Mail_Queue'd 50,000+ without issue
Alister Bulman