Notifications

This chapter describes:

1 Introduction

The portal provides a global functionality for handling notifications.

Each modules has to use this general functionality for notification and mail sending.

With this method users can unsubscribe from notifications globally and we can introduce new notification methods (SMS etc).

Users can define if they want to receive messages per mail in the portal. Users can define the language of notifications.

2 How it works

Any notification in the portal is represented by an instance of the Notification class.

Notification class store:

  • subject
  • message
  • recipients
  1. Subject and message is a text fields.
  2. Recipients is an array of receivers who should get notifications.
  3. Each recipient represents an object.

For example email recipient are represented by EmailRecipient class. This class stores name and email of recipient.

In future we can create other recipient types, for example PhoneRecipient which will store the user phone. Or FileRecipient which will store address of a remote file.

Recipients array can contain mixed type of recipients.

Any notification is handled by Notificators ancestors.

Notificator – is an abstract class. It`s ancestors get all notifications which occurred in the system and handle only that one which are succeed by internal Notificator algorithm.

For example MailNotificator handles all notifications which have an “EmailRecipient” in its recipient array.

There is a singleton object, which is an instance of NotificationManager class, which connecst Notification with Notificators.

3 Configuration

NotificationManager configured in main.php


 'notificationManager' => array(
             //cache time for user model
             'cacheTime' => 60 * 60,
             //notificators for handling notificators
             'notificators' => array(
                 'mail' => array(
                     //notificator class
                     'class' => 'common.components.notification.MailNotificator',
                     //PhpMailer extension options
                     'phpMailer' => array(
                         'CharSet' => 'UTF-8',
                         'Encoding' => 'base64',
                     )
                 ),
                 'file' => array(
                     //notificator class
                     'class' => 'common.components.notification.FileLogNotificator',
                 )
             )
         ),

3.1 Using Notifications with MailNotificator


$notification = new Notification(‘subject’,’message’));
$notification->recipients[] = new EmailRecipient($email);
Yii::app()->notificationManager->notifyAbout($notification);

3.2 Using Notifications with FileLogNotificator

FileLogNotificator by default catches all notifications and write them to file specified in


'components'=>
        //logger settings
        'log' => array(
            'routes' => array(
                'notificationLog' => array(
                    'class' => 'CFileLogRoute',
                    'logPath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'custom/runtime',
                    'logFile' => 'FileLogNotificator.log',
                    'categories' => 'FileLogNotificator'
                ),

4 Mail sending

MailNotificator uses PhpMailer for sending emails.

PHPMailer is a PHP class that provides a package of functions to send email. The two primary features are sending HTML email and emails with attachments.

PHPMailer supports nearly all possiblities to send email: mail(), Sendmail, qmail & direct to SMTP server. You can use any feature of SMTP-based email, multiple recepients via to, CC, BCC, etc. In short: PHPMailer is an efficient way to send emails within PHP.

Class is placed in common/extensions/PHPMailer.php.

Each message template must be customizable (client specific) and international (I18N).