zee zee - 1 month ago 5x
PHP Question

How can I send a daily email notification to users in PHP?

I have a simple user signup form with a checkbox that lets users get a daily email notification if there was activity on any of their projects...much like Stack Overflow has a "Notify xxx@example.com daily of any new answers".

My current thinking to implement this in a LAMP environment is as follows:

  1. In the user database, set a boolean value if the user wishes to receive a daily email.

  2. If there is any project activity, the project gets updated with the current timestamp.

  3. Each night (midnight), a PHP file is executed (likely through a cron job) that scans through the project database to identify which projects had activity that day. For projects with activity, the project owner name is selected and the user table is scanned to check if the user wishes to receive a daily email notification. If yes, add to a recipient list; else, ignore.

Questions / concerns I have that would appreciate some guidance on before I start to implement:

  1. I'm in a shared hosting environment. What precautions do I need to take from being misidentified as spam either by the hosting company or the receiving mail servers?

  2. Do I need to "chunk" out the recipient list (50 emails at a time) and email each group? Is this as simple as putting a sleep(30); between each call to mail()?

  3. I'm using the CodeIgniter framework and will have the cron job call the appropriate function in a controller to run this at midnight. How do I limit calls from only the cron job to prevent some unauthorized user from calling this function from the browser?


  1. If you do change the "From" header in php, make sure you change it to the domain that's hosted on that server. It looks suspicious when mail @a.com is being sent by b.com's servers.

  2. I would send the emails individually foreach ($Users as $User)..., since this allows you to personalize the email contents. Even if you don't need to personalize emails now, you might want to later, and the support for it will already be there when you need it.

  3. First, I would store the script outside of the web root. I'm not sure if CodeIgniter will let you do this, but there is no need for the script to ever be served by Apache. Cron doesn't care where the script is stored. Additionally, I've checked the time when the script is executed. If it's not midnight, then don't blast out the emails. Also, you could keep a log around and also check to make sure the emails haven't already been sent that day before sending.