芝麻web文件管理V1.00
编辑当前文件:/home/mgatv524/vendashop.mgaplay.com.br/lib/XTR/EmailNotificationsTask.php
. */ namespace Xibo\XTR; use Carbon\Carbon; use Slim\Views\Twig; use Xibo\Factory\UserNotificationFactory; /** * Class EmailNotificationsTask * @package Xibo\XTR */ class EmailNotificationsTask implements TaskInterface { use TaskTrait; /** @var Twig */ private $view; /** @var UserNotificationFactory */ private $userNotificationFactory; /** @inheritdoc */ public function setFactories($container) { $this->view = $container->get('view'); $this->userNotificationFactory = $container->get('userNotificationFactory'); return $this; } /** @inheritdoc */ public function run() { $this->runMessage = '# ' . __('Email Notifications') . PHP_EOL . PHP_EOL; $this->processQueue(); } /** Process Queue of emails * @throws \PHPMailer\PHPMailer\Exception */ private function processQueue() { // Handle queue of notifications to email. $this->runMessage .= '## ' . __('Email Notifications') . PHP_EOL; $msgFrom = $this->config->getSetting('mail_from'); $msgFromName = $this->config->getSetting('mail_from_name'); $processedNotifications = []; $this->log->debug('Notification Queue sending from ' . $msgFrom); foreach ($this->userNotificationFactory->getEmailQueue() as $notification) { $this->log->debug('Notification found: ' . $notification->notificationId); if (!empty($notification->email) || $notification->isSystem == 1) { $mail = new \PHPMailer\PHPMailer\PHPMailer(); $this->log->debug('Sending Notification email to ' . $notification->email); $mail->addAddress($notification->email); // System notifications, add mail_to to addresses if set. if ($notification->isSystem == 1) { // We should send the system notification to: // - all assigned users // - the mail_to (if set) $mailTo = $this->config->getSetting('mail_to'); // Make sure we've been able to resolve an address. if (empty($notification->email) && empty($mailTo)) { $this->log->error('Discarding NotificationId ' . $notification->notificationId . ' as no email address could be resolved.'); continue; } // if mail_to is set and is different from user email, and we did not // process this notification yet (the same notificationId will be returned for each assigned user) // add it to addresses if ($mailTo !== $notification->email && !empty($mailTo) && !in_array($notification->notificationId, $processedNotifications) ) { $this->log->debug('Sending Notification email to mailTo ' . $mailTo); $mail->addAddress($mailTo); } } // Email them $mail->CharSet = 'UTF-8'; $mail->Encoding = 'base64'; $mail->From = $msgFrom; // Add attachment if ($notification->filename != null) { $mail->addAttachment( $this->config->getSetting('LIBRARY_LOCATION') . 'attachment/' . $notification->filename, $notification->originalFileName ); } if (!empty($msgFromName)) { $mail->FromName = $msgFromName; } $mail->Subject = $notification->subject; $addresses = explode(',', $notification->nonusers); foreach ($addresses as $address) { $mail->AddAddress($address); } // Body $mail->isHTML(true); $mail->AltBody = $notification->body; $mail->Body = $this->generateEmailBody($notification->subject, $notification->body); if (!$mail->send()) { $this->log->error('Unable to send email notification mail to ' . $notification->email); $this->runMessage .= ' - E' . PHP_EOL; $this->log->error('Unable to send email notification Error: ' . $mail->ErrorInfo); } else { $this->runMessage .= ' - A' . PHP_EOL; } $this->log->debug('Marking notification as sent'); } else { $this->log->error('Discarding NotificationId ' . $notification->notificationId . ' as no email address could be resolved.'); } $processedNotifications[] = $notification->notificationId; // Mark as sent $notification->setEmailed(Carbon::now()->format('U')); $notification->save(); } $this->runMessage .= ' - Done' . PHP_EOL; } /** * Generate an email body * @param $subject * @param $body * @return string */ private function generateEmailBody($subject, $body) { // Generate Body // Start an object buffer ob_start(); // Render the template echo $this->view->fetch('email-template.twig', ['config' => $this->config, 'subject' => $subject, 'body' => $body]); $body = ob_get_contents(); ob_end_clean(); return $body; } }