Email queueing in php

妖精的绣舞 提交于 2019-12-04 08:20:35

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.

There's a tested solution for that: PEAR Mail_Queue

Works fine for me.

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 set_time_limit() 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

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.

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!

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!