how can i send a daily email notification to users in php

how can i send a daily email notification to users in php  using -'php,email,codeigniter,notifications'

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 daily of any new answers".

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

In the user database, set a boolean value if the user wishes to receive a daily email.
If there is any project activity, the project gets updated with the current timestamp.
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:

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?
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()?
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?


asked Sep 15, 2015 by JWYGraceiwqh
0 votes

4 Answers

0 votes
  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 is being sent by'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.

answered Sep 15, 2015 by EugenioLawle
0 votes

1) Start with an SPF record and a DKIM if you can that lets mail servers know to expect email from your servers

2) First, you need to put the recipients in the BCC field so that it not each user has the email addresses of 49 other users on your system. One step further is to do each email separately, putting only the recipient in the TO field. This approach also allows your to tailor each email to the user (perhaps putting in "Hi [First name]".

3) Have the cron job something like this wget http://localhost/send-emails

Then in your script, check $_SERVER and make sure you only allow requests from

answered Sep 15, 2015 by TomCrabtree
0 votes

About the third question: You can either use an .htaccess file to prevent access to that specific page or you can call your script in cron with a command line parameter and check for that variable in $argv.

answered Sep 15, 2015 by EllEmmer
0 votes

1) The SPF record is the most important thing. Use the email from the domain so, where has the SPF records set correctly.

2) It's always good to throttle email, especially when first starting out. You should check your shared servers policies, which are normally 200-500/hour. Calculate how many seconds that comes to. For example 300/hour is 1 every 12 seconds. After a few weeks of sending good emails, you should be ok to send larger amounts.

3) You can have the cron file outside the webroot or limit access via .htaccess or another method.

answered Sep 15, 2015 by Peggy7848yuo