How to send e-mail reminders to users in yii framework

To send out these reminders, we’ll create a new console command called RemindersCommand in protected/commands/RemindersCommand.php. When we’re done, we’ll be able to add this command to either our crontab or to our scheduled tasks and have it automatically process reminders in the background.

Once the RemindersCommand file has been created, create the class definition in addition to an action to send the reminders that takes a time interval as an argument. This interval will define the length of time in minutes that we should run our command for. It will find all of the reminders within that interval’s timeframe to process:

class RemindersCommand extends CConsoleCommand
   public function actionSendReminders($interval) {}

Within our action, define the UNIX timestamp that we should begin at as well as the time we should end at for the particular interval we are working with. The end time should be all microseconds before the next interval begins so that we do not send duplicate reminders out:

$time = time();
$start = $time - ($time % $interval * 60);
$end = $start + (($interval *60) - 1));

With our time interval defined, we can now create a database search criteria with CDBCriteria that we can pass to our reminders find() method:

$criteria = new CDbCriteria;
$criteria->addBetweenCondition('offset', $start, $end);
$reminders = Reminders::model()->findAll($criteria);

The find() method will return all reminders within the time interval that we specified. We can now simply iterate through the $reminders array and send an e-mail to the user the reminder belongs to:

foreach ($reminders as $reminder)
   // Load the PHPMailer Class
   $mail = new PHPMailer;

   // Tell PHP Mailer to use SMTP with authentication
   $mail->SMTPAuth = true;

      // Specify the Host and Port we should connect to
   $mail->Host = Yii::app()->params['smtp']['host'];
   $mail->Port = Yii::app()->params['smtp']['port'];

   // Specify the username and password we should use
   // (if required by our SMTP server)
   $mail->Username = Yii::app()->params['smtp']['username'];
   $mail->Password = Yii::app()->params['smtp']['password'];

   // Set the security type of required
   $mail->SMTPSecure = 'tls';

   // Set the from and to addresses
   $mail->from = Yii::app()->params['smtp']['from'];

   // This should be an HTML email

   // Set the subject and body
   $mail->Subject ='Reminder from Scheduled Reminders';
   $mail->Body = 'This is a reminder that '.$reminder->event->title.' is due on '. gmdate("Y-m-d H:i UTC", $reminder->offset) . '. This event has the following details:<br />' . $reminder->event->data;

   // Then send the email
   if (!$mail->send())
        echo $mail->ErrorInfo . "\n";
        echo ".";


If you are using a remote SMTP server and have already populated your protected/config/params.php file with your SMTP information, the previous code should work for you. If you’re using a local mail server, such as Postfix or another setup, be sure to read the PHPMailer documentation at on how to propery configure PHPMailer.

Recommended :  5 Best Java Decompilers

From the command line, we can now send reminders by running the following command (in the example we are using a 5-minute interval):

php protected/yiic.php reminders sendreminder --interval=5

Once you have created events in your database, you can run the command or put this command on your crontab or scheduled tasks and have your application automatically send reminders to your users.

Pin It on Pinterest

Scroll Up

Get the best in web dev

Join and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)