无需 cron 在后台发送邮件
我想知道是否有一种方法可以运行 PHP 循环,以便在后台向订阅者发送几百封电子邮件。我的目标是格式化新闻通讯,单击发送,然后关闭浏览器或更改页面。当然,发送电子邮件的实际过程将在后台运行,不会因浏览器关闭而中断。
我知道这可以通过 cron 作业从 MySQL 或文本文件中保存的队列读取来完成,但是这样,即使很长一段时间没有队列,cron 也会一直运行,寻找队列......
我在一个名为 Pommo (https://github.com/soonick/poMMo) 的脚本中看到了这种功能,但似乎无法理解它是如何完成的。
有人对此有想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我本来打算对您的问题添加评论,但我没有足够的空间来格式化并给出示例。
我认为这是一个可行的想法:
1 - 加载您想要发送到数据库或文件的所有电子邮件。
2 - 从您的网络应用程序中单击按钮发送电子邮件。这将向服务器提交 Ajax 请求。您可以定义要在特定时间范围内发送的电子邮件数量。请记住,大多数主机对每小时可以发送的电子邮件数量有限制。
3 - 创建一个 php 脚本,该脚本将接收 Ajax 请求并发送您定义的参数内的所有电子邮件。
4 - 我相信您可以关闭您的网络浏览器,因为 PHP 脚本将遍历整个列表,并且在完成发送所有电子邮件之前不会返回。
上面的方法可能有效,但我永远不会这样做。我会使用如上所述的 cronjob。您的 cronjob 只需检查是否有电子邮件要发送。这不是资源密集型的。
如果您决定实施上述想法,请务必告知我们。我很好奇这是否有效。
祝你好运!
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!
这几乎超出了 Cron 的目的。您应该创建一个作业,例如每 15 分钟运行一次,并检查队列中是否有需要发送的邮件。如果没有邮件,就让脚本死掉,它会在 15 分钟后再次运行。
如果有要发送的邮件,请在开始发送之前更新行以指示您正在处理这些邮件,因此运行时间超过 15 分钟不会导致另一个脚本实例发送相同的邮件。
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.
你需要一个排队系统。例如,Linux 上有 Beanstalkd,你可以用 php 来提供东西。
You need a queue system. There is e.g. Beanstalkd for linux, which you would feed things with php.