Hi everyone. Am trying to send email reminder for users to update their progress 1 month before end of every quarter. So every February and May etc the email notification will be autosend to the users from the system. Below is the code. Please advise if this is the correct way of doing it or if there is any better way. Thanks a lot.

    <?php 
     $month= $row['MONTH(CURRENT_DATE())'];

    //get email address's
     $con=mysqli_connect("localhost","user","","pq");
    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT email FROM tuser"); 

    while($row = mysqli_fetch_array($result))
    {

     if('$month'==2){     
     $to=$row['name']." <".$row['email'].">\r\n";
     $subject="Please find this friendly reminder to update your Operational progress.";
     $headers  = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
     $headers .="To: ".$row['name']." <".$row['email'].">\r\n";
     $headers .= "From: system <system@edu.com>" . "\r\n";
     mail($to, $subject, $email, $headers);
     }
     elseif('$month'==5){     
    $to=$row['name']." <".$row['email'].">\r\n";
     $subject="Please find this friendly reminder to update your Operational progress.";
     $headers  = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
     $headers .="To: ".$row['name']." <".$row['email'].">\r\n";
     $headers .= "From: system <system@edu.com>" . "\r\n";
     mail($to, $subject, $email, $headers);
     }
    } 
    mysqli_close($con); 
     ?> 

Dani AI

Generated

To make this truly automatic, do not tie the reminder to a page view (like login). Schedule a PHP script with cron so it runs on a predictable date/time, and only sends during the months that are one month before quarter end: Feb, May, Aug, Nov. Also ensure you send only once per period (store a row in a log table keyed by user_id and period, e.g., 2025Q1), so re-runs do not duplicate messages.

Example skeleton you can run daily via cron:

<?php
date_default_timezone_set('UTC'); // pick the timezone that matches your business rules

$month = (int)date('n'); // 1..12
$day   = (int)date('j'); // 1..31

// Only run on 1st day of Feb, May, Aug, Nov
if (!in_array($month, [2, 5, 8, 11], true) || $day !== 1) {
    exit;
}

// TODO: connect to DB, fetch active users
// TODO: for each user, check a "reminder_log" table to avoid duplicates for this period
// TODO: compose $subject and $body, then send using a mailer library
// TODO: insert a log row (user_id, period, sent_at)

Cron example (runs 09:00 on the 1st of Feb/May/Aug/Nov):
0 9 1 2,5,8,11 * /usr/bin/php /path/to/send_quarter_reminders.php

Practical tips:

  • Compute the period label from today, e.g., 2025Q1 for reminders sent in Feb (before Q1 end). This makes deduping easy.
  • Prefer a library like PHPMailer or Symfony Mailer for proper headers and SMTP handling; see PHPMailer docs and PHP mail configuration notes.
  • Be explicit about timezones to avoid off-by-one-day issues; see PHP date/time.

Recommended Answers

All 5 Replies

Hi,

small note: in your IF statementes if('$month'==2) (and co.) you are comparing literally the word $month, not his value, against the integer. Single quotes will NOT parse the variable, so in this case you can remove them and write:

if($month == 2)

Or with double quotes:

if("$month" == 2)

Double quotes can parse a variable and expand it to the his value. Read the documentation for more info:

Hi cereal, thanks a lot for your reply. Currently am not sure where to place the above coding. Appreciate if you could advise how to implement autosend for this webpage. Thanks a lot.

I'm referring at the IF/ELSEIF statements at lines 17 and 26.

Thanks cereal, so where to put the above coding for auto reminder email. Should i put it at the login page or how? Thanks.

Should i put it at the login page or how?

It depends, this will send an email only to those clients that are signing in that specific period, if this is your goal then it can work otherwise it would be better to schedule the task, if the latter is the case then check if your hosting control panel allows you to set cron job scripts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.