PHP:添加计时器
我正在创建一个邮件列表应用程序。由于邮件服务的限制,每小时将发送 40 封电子邮件。如何添加定时器?
I'm creating a mailing list application. It will send 40 emails per hour due to the restrictions of the mailing service. How can I add the timer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您需要创建计划任务,我建议创建一个 Cron Job。
If you need to create a scheduled task, I would suggest creating a Cron Job.
您可以使用
sleep()
暂停脚本功能。但这不是一个好的解决方案。更好的方法是使用 Cron,它允许您每 40 分钟执行一次脚本。脚本本身只需检查到目前为止已经执行了多少次 - 该信息可以存储在某个文件中:You can suspend script using
sleep()
function. However it's not a good solution. Much better approach is to use Cron that allows you to execute script once every 40 minutes. The script itself has to only check how many times it has been executed so far - that information can be stored in some file:我发现了这个: http://swiftmailer.org/docs/throttler-plugin
I found this: http://swiftmailer.org/docs/throttler-plugin
一种解决方案是创建一个数据库表来保存您已发送的电子邮件的详细信息。假设它看起来像这样:
这里
email_id
应该是包含您标记为发送的电子邮件的表的外键,而recipient_id
应该是包含收件人详细信息的表。time_sent
显然记录了电子邮件的发送时间。现在,当您想要发送电子邮件时,您会想知道
COUNT
查询找到,对此的一种可能的变化是,在表中为您打算发送的每个电子邮件-用户组合添加一行,而不是为迄今为止发送的每封电子邮件单独添加一行,并有一列指示该电子邮件是否已发送给该用户。这样就可以很容易地知道您还需要向谁发送电子邮件。
正如其他人所建议的,您可以将此方法与 Cron 作业结合起来,以确保定期处理电子邮件队列。
One solution would be to create a database table to hold details of the emails you have sent. Let's say it looks like this:
Here
email_id
should be a foreign key to a table containing the emails you've marked for sending, andrecipient_id
should be a foreign key to a table containing details of your recipients.time_sent
obviously records the time the email was sent.Now when you want to send emails, you will want to know
COUNT
queryA possible variation on this is to have a row in the table for every email-user combination you intend to send, rather than just one for every email sent so far, and have a column indicating whether that email has been sent to that user yet. That would make it easy to tell also who you still have to send an email to.
You would combine this approach with a Cron job, as suggested by others, to make sure the queue of emails is dealt with regularly.