/**
  * Send a notification directly to a single user
  *
  * @param SystemNotification $notification
  * @param string $email
  * @param array $data
  */
 public function sendToUser($notification, $context, $user, $data)
 {
     $subject = $notification->format($notification->Title, $context, $user, $data);
     if (Config::inst()->get('SystemNotification', 'html_notifications')) {
         $message = $notification->format($notification->NotificationContent(), $context, $user, $data);
     } else {
         $message = $notification->format(nl2br($notification->NotificationContent()), $context, $user, $data);
     }
     if ($template = $notification->getTemplate()) {
         $templateData = $notification->getTemplateData($context, $user, $data);
         $templateData->setField('Body', $message);
         try {
             $body = $templateData->renderWith($template);
         } catch (Exception $e) {
             $body = $message;
         }
     } else {
         $body = $message;
     }
     $from = $this->config()->get('send_notifications_from');
     $to = $user->Email;
     if (!$to && method_exists($user, 'getEmailAddress')) {
         $to = $user->getEmailAddress();
     }
     // log
     $message = "Sending {$subject} to {$to}";
     SS_Log::log($message, SS_Log::NOTICE);
     // send
     $email = new Email($from, $to, $subject);
     $email->setBody($body);
     $this->extend('onBeforeSendToUser', $email);
     $email->send();
 }
 public function testSendEmailNotification()
 {
     $notification = new SystemNotification();
     $notification->Title = "Notify on event";
     $notification->Description = 'Notifies on an event occurring';
     $notification->NotificationText = 'This is a notfication to $Member.Email about $NotifyOnThis.Title';
     $notification->Identifier = 'NOTIFY_ON_EVENT';
     $notification->NotifyOnClass = 'NotifyOnThis';
     $notification->write();
     // okay, add it to our page
     $page = $this->objFromFixture('NotifyOnThis', 'not1');
     $ns = new NotificationService();
     Config::inst()->update('NotificationService', 'use_queues', false);
     Config::inst()->update('EmailNotificationSender', 'send_notifications_from', '*****@*****.**');
     Config::inst()->update('SystemNotification', 'default_template', false);
     $ns->setSenders(array('email' => 'EmailNotificationSender'));
     $ns->setChannels(array('email'));
     $ns->notify('NOTIFY_ON_EVENT', $page);
     // now check that there was an email sent
     $users = $page->getRecipients($notification->Identifier);
     $expectedTo = $users[0]->Email;
     $expectedFrom = '*****@*****.**';
     $expectedSubject = $notification->Title;
     $expectedBody = "This is a notfication to {$expectedTo} about {$page->Title}";
     $expectedBody = $notification->format(nl2br($expectedBody), $page);
     // TODO
     $this->assertEmailSent($expectedTo, $expectedFrom, $expectedSubject);
 }