JDavies
JDavies

Reputation: 2770

Send weekly emails for a limited time after sign up in django

The Goal: Once a user has signed up they start a challenge. Once they've started the challenge I'd like to be able to send users weekly emails for 12 weeks (Or however long the challenge lasts for). Bearing in mind, one user may sign up today and one may sign up in three months time. Each week, the email will be different and be specific to that week. So I don't want the week one email being sent alongside week 2 for 12 weeks.

Should I add 12 boolean fields inside the extended User model, then run checks against them and set the value to True once that week gas passed?

Assuming this is the case. Would i then need to setup a cron task which runs every week, and compares the users sign up date to todays date and then checks off the week one boolean? Then use the post_save signal to run a check to see which week has been completed and send the 'week one' email?

I hope that makes sense and i'm trying to get my head around the logic. I'm comfortable with sending email now. It's trying to put together an automated process as currently it's all manual.

Please let me know if I need to elaborate on anything. Any help would be much appreciated.

Upvotes: 1

Views: 806

Answers (2)

shlomta1
shlomta1

Reputation: 148

You definitely need to use cron task - I recommend Celery. I think that you just need to build 12 templates (for every week, or maybe building less and use different parameters in the message) in your mailing service and name them with a format (for example {challenge_name}-{week_num}). Every week when your cron task runs target the email message that you want to send by calculating the number weeks from the user registration date and planting it in your format to choose the correct email.

Upvotes: 0

campovski
campovski

Reputation: 3153

The easiest way would be something like this:

  • Make a table e.g. challenge in database with following cols: challenge_name, week1, week2, ..., as much weeks as you need. Make them nullable, so if some challenge is shorter, the rest of weeks can be null.
  • In users table (or you can make new one) add two cols, one for active challenge, the other for day started.
  • Then yes, you can run cron job daily, or maybe twice a day, in the morning and in the afternoon, that executes python function for sending mail. In that function you go through all users, check their current challenge, calculate the week they are in, query table challenge for mail content and then send.

I think this is the best solution, certainly the most simple and solid one :)

Upvotes: 2

Related Questions