PHP:添加计时器

发布于 2024-09-25 04:20:42 字数 57 浏览 3 评论 0原文

我正在创建一个邮件列表应用程序。由于邮件服务的限制,每小时将发送 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不可一世的女人 2024-10-02 04:20:42

如果您需要创建计划任务,我建议创建一个 Cron Job

If you need to create a scheduled task, I would suggest creating a Cron Job.

就此别过 2024-10-02 04:20:42

您可以使用 sleep() 暂停脚本功能。但这不是一个好的解决方案。更好的方法是使用 Cron,它允许您每 40 分钟执行一次脚本。脚本本身只需检查到目前为止已经执行了多少次 - 该信息可以存储在某个文件中:

$counter = file_exists('counter') ? file_get_contents('counter') : 1;

echo sprintf('Send %d portion of emails.', $counter);

file_put_contents('counter', ++$counter);

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:

$counter = file_exists('counter') ? file_get_contents('counter') : 1;

echo sprintf('Send %d portion of emails.', $counter);

file_put_contents('counter', ++$counter);
舂唻埖巳落 2024-10-02 04:20:42

一种解决方案是创建一个数据库表来保存您已发送的电子邮件的详细信息。假设它看起来像这样:

CREATE TABLE SentEmail (
    email_id INT NOT NULL,
    recipient_id INT NOT NULL,
    time_sent DATETIME NOT NULL,
    PRIMARY KEY (email_id, recipient)
)

这里 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:

CREATE TABLE SentEmail (
    email_id INT NOT NULL,
    recipient_id INT NOT NULL,
    time_sent DATETIME NOT NULL,
    PRIMARY KEY (email_id, recipient)
)

Here email_id should be a foreign key to a table containing the emails you've marked for sending, and recipient_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

  • how many emails have been sent this hour; this can be found using a COUNT query
  • who has already been sent the email you're currently dealing with; this is also a simple query.

A 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文