I've got a website in which I want to send some followup emails to customers a certain number of days after they bought something. I now wonder how to do that. I think there are two options:
My question is actually; what is best practice in this case? How do most websites handle this and why?
I would choose method 2.
The disadvantage is not really a disavantage. Supposing you got an "order" table, you can get the list of mail to send just making a query quite similar the one used by your cron.
But it is a personal choice. I don't know which method is normally used.
I would go on a combination of both options, and that is the method I actually doing so in a system I'm currently developing.
Having a "ready to send" list is useful for logging and tracking your emails, for example, if you use a third party emailing solution, and you have a limited number of emails per month, you can track the amount you used from within your program, and maybe even trigger an automatic "upgrade" of the account if required because you need more emails.
The required flexibility can be achieved by designing a good schema for that table.
The solution you described will have a schema like so I guess:
|---------|---------|------|---------|-------|
| send_to | subject | body | send_at | sent |
|---------|---------|------|---------|-------|
That is really not flexible, because once inserted into the database, in order to change the send_at
column you will have to retrieve data from orders, and recalculate the send_at
value.
I propose a schema like so:
|---------|---------|------|-----------|---------|-------|
| send_to | subject | body | added_at | send_in | sent |
|---------|---------|------|-----------|---------|-------|
The change is that the send_at
column is not fixed now. When you run the cron
, you will retrieve only emails that match the following query:
!sent && added_at + send_in >= now
This will return the same result as querying the first schema using:
!sent && send_at >= now
But now, you can easily change the duration of the waiting between the time that the email is added to the queue and the time that it actually will be send.