Source of file EmailNotificationSender.php
Size: 2,419 Bytes - Last Modified: 2021-12-23T10:20:21+00:00
/var/www/docs.ssmods.com/process/src/code/services/EmailNotificationSender.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 | <?php /** * EmailNotificationSender * @author marcus@silverstripe.com.au, shea@livesource.co.nz * @license http://silverstripe.org/bsd-license/ */ class EmailNotificationSender extends Object implements NotificationSender { /** * Email Address to send email notifications from * @var string */ private static $send_notifications_from; /** * Send a notification via email to the selected users * * @param SystemNotification $notification * @param DataObject $context * @param array $data */ public function sendNotification($notification, $context, $data) { $users = $notification->getRecipients($context); foreach ($users as $user) { $this->sendToUser($notification, $context, $user, $data); } } /** * 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(); } } |