views:

24

answers:

4

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?

+3  A: 

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

Russell Dias
+1  A: 

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);
Crozin
A: 

I found this: http://swiftmailer.org/docs/throttler-plugin

omgzor
A: 

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.

Hammerite