Send mails in background without cron

空扰寡人 提交于 2019-12-31 05:19:45

问题


I was wondering if there is a way to run a PHP loop in order to send a few hundred emails to subscribers in background. My goal is to format the newsletter, click send and then close the browser or change page. Of course, the actual process of sending the e-mail will be running in background and would not be interrupted by the browser closing.

I know this can be made with a cron job reading from a queue saved in MySQL or text file, but this way, even if there is no queue for a long period, the cron will always be running, looking for the queue...

I've seen this funcionality in a script called Pommo (https://github.com/soonick/poMMo) but can't seem to understand how it's done.

Does anyone have an idea for this?


回答1:


I was going to add a comment to your question, but them I didn't have enough space there to format and give the example.

Here is an idea I believe might work:

1 - Load all the emails you want to send to a database or file.

2 - From your web application click on the button to send emails. This will submit an Ajax request to the server. You can define the number of emails you want to send within a certain timeframe. Remember that most hosts have limits on number of emails you can send every hour.

3 - create a php script that will receive the Ajax request and send all the emails within the parameters you define.

4 - I believe you can kill your web browser because the PHP script will run through the whole list and will not return until it finishes sending all the emails.

The above might work, however I would never do it this way. I would use a cronjob as stated above. Your cronjob would only have to check if there are emails to send or not. This is not resource intensive.

If you decide to implement the ideas above, please make sure you let us know. I am curious if that would work.

Good luck!




回答2:


I know this can be made with a cron job reading from a queue saved in MySQL or text file, but this way, even if there is no queue for a long period, the cron will always be running, looking for the queue...

That pretty much beats the purpose of Cron. You should create a job that runs, say, every 15 minutes and checks the queue for mails that need to be sent. If there are no mails, let the script die, it'll run again in 15 minutes.

If there are mails to be sent, update the rows to indicate that you're processing them before you start sending, so a run taking more than 15 minutes won't cause another script instance to send the same mails.




回答3:


You need a queue system. There is e.g. Beanstalkd for linux, which you would feed things with php.



来源:https://stackoverflow.com/questions/7333882/send-mails-in-background-without-cron

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