How to send daily email notification to users in PHP?

I have a simple user registration form with a checkbox that allows users to receive daily email notifications if there was activity for any of their projects ... just like Qaru has "Notify xxx@example.com daily of any new replies" .

My current thinking for implementing this in a LAMP environment is as follows:

  • In the user database, set a boolean value if the user wants to receive daily email.

  • If there is any project activity, the project is updated with the current time stamp.

  • Every night (midnight), a PHP file is executed (probably through a cron job), which looks through the project database to determine which projects were active on that day. For projects with activity, the name of the project owner is selected, and the user table checks if the user wants to receive a daily notification by e-mail. If yes, add to the recipient list; else, ignore.

The questions / issues that I have would be appreciated by some recommendations before I start implementing:

  • I work in a shared hosting environment. What precautions should I take from a mis-definition as spam by either the hosting company or the receiving mail servers?

  • Do I need to “cut out” the recipient list (50 emails at a time) and email each group? It is as simple as sleeping (30); between each call to the mail ()?

  • I use the CodeIgniter structure and get the cron call of the corresponding function in the controller to run this at midnight. How to limit calls only to the cron task to prevent the user from unauthorized calling this function from the browser?

Thanks.

+6
source share
4 answers
  • If you change the "From" header in php, make sure you change it to a domain hosted on this server. This looks suspicious when mail @ a.com is sent by b.com servers.

  • I would send emails individually foreach ($Users as $User)... since this allows you to personalize the contents of the email. Even if you do not need to personalize emails now, you may want to later, and support for it will already be there when you need it.

  • Firstly, I would save the script outside the root of the website. I'm not sure that CodeIgniter will let you do this, but I don't need the script to ever be served by Apache. Cron doesn't care where the script is stored. In addition, I checked the execution time of the script. If it's not midnight, then don't blow up emails. In addition, you can save the journal and check if emails are sent that day before sending.

+1
source

1) Start by recording SPF and DKIM, if possible, which allows mail servers to know that email is coming from your servers

2) First you need to put the recipients in the BCC field so that each user does not have the email address of 49 other users in your system. Another step is to do each email separately, placing only the recipient in the TO field. This approach also allows you to customize each email to the user (perhaps insert "Hello [Name]".

3) Define a cron task something like this: wget http://localhost/send-emails

Then, in the script, mark $_SERVER and make sure that you only allow requests from 127.0.0.1

+1
source

About the third question: you can use the .htaccess file to prevent access to this particular page or you can call the script in cron with a command line parameter and check this variable in $argv .

+1
source

1) Recording SPF is the most important thing. Use email from a domain, so contact@whatever.com, where what.com has the correct SPF record.

2) It is always useful to throttle e-mail, especially at the first start. You should check the policies of shared servers, which are usually 200-500 / hour. Calculate how many seconds this will happen. For example, 300 / hour - 1 time in 12 seconds. After weeks of sending good emails, you should be fine to send large amounts.

3) You may have a cron file outside of webroot or restrict access via .htaccess or another method.

+1
source

Source: https://habr.com/ru/post/888341/


All Articles