userManager->get($recipientId); if (empty($recipient)) { $this->logger->error("recipient $recipientId is null"); return false; } $email = $recipient->getEMailAddress(); if (empty($email)) { $this->logger->info("The mentioned recipient $recipientId does not have an email"); return false; } $recipientName = $recipient->getDisplayName(); $notifier = $this->userManager->get($notifierId); if (empty($notifier)) { $this->logger->error("notifier $notifierId is null"); return false; } $notifierName = $notifier->getDisplayName(); $editorLink = $this->urlGenerator->linkToRouteAbsolute($this->appName . ".editor.index", [ "fileId" => $fileId, "anchor" => $anchor ]); $notifierLink = $this->urlGenerator->linkToRouteAbsolute('core.ProfilePage.index', ['targetUserId' => $notifierId]); $subject = $this->trans->t("You were mentioned in the document"); $heading = $this->trans->t("%1\$s mentioned you in the document comment", [$notifierName]); $bodyHtml = $this->trans->t( "This is a mail message to notify that you have been mentioned by %2\$s in the comment to the %4\$s:
\"%5\$s\"", [$notifierLink, $notifierName, $editorLink, $fileName, $notificationObjectId] ); $this->logger->debug($bodyHtml); $button = [$this->trans->t("Open file"), $editorLink]; $template = $this->buildEmailTemplate($subject, $heading, $bodyHtml, $button); $result = $this->sendEmailNotification($template, $email, $recipientName); if ($result) { $this->logger->info("Email to $recipientId was sent"); } return $result; } /** * Send notification about editors unsuccessfull check via email * * @param string $uid - user id * * @return bool */ public function notifyEditorsCheckEmail(string $uid): bool { $user = $this->userManager->get($uid); if (empty($user)) { $this->logger->error("recipient $uid is null"); return false; } $email = $user->getEMailAddress(); if (empty($email)) { $this->logger->info("The notification recipient $uid does not have an email"); return false; } $userName = $user->getDisplayName(); $subject = $this->trans->t("ONLYOFFICE Document Server is unavailable"); $bodyHtml = $this->trans->t("This is a mail message to notify that the connection with the ONLYOFFICE Document Server has been lost. Please check the connection settings:"); $appSettingsLink = $this->urlGenerator->getAbsoluteURL("/settings/admin/".$this->appName); $button = [$this->trans->t("Go to Settings"), $appSettingsLink]; $template = $this->buildEmailTemplate($subject, $subject, $bodyHtml, $button); $result = $this->sendEmailNotification($template, $email, $userName); if ($result) { $this->logger->info("Email to $uid was sent"); } return $result; } /** * Build email template * * @param string $subject - e-mail subject text * @param string $heading - e-mail heading text * @param string $body - e-mail body html * @param array $button - params for NC-button (0-text, 1-link) * * @return IEMailTemplate */ private function buildEmailTemplate( string $subject, string $heading, string $body, array $button = [] ): IEMailTemplate { $template = $this->mailer->createEMailTemplate("onlyoffice.NotifyEmail"); $template->setSubject($subject); $template->addHeader(); $template->addHeading($heading); $template->addBodyText($body, true); if (!empty($button) && isset($button[0]) && isset($button[1]) && is_string($button[0]) && is_string($button[1])) { $template->addBodyButton($button[0], $button[1]); } $template->addFooter(); return $template; } /** * Send email * * @param IEMailTemplate $template - e-mail template * @param string $email - e-mail address * @param string $recipientName - recipient name */ private function sendEmailNotification(IEMailTemplate $template, string $email, string $recipientName): bool { try { $message = $this->mailer->createMessage(); $message->setTo([$email => $recipientName]); $message->useTemplate($template); $errors = $this->mailer->send($message); if (!empty($errors)) { $this->logger->error("Email service error: " . json_encode($errors)); return false; } } catch (\Exception $e) { $this->logger->error("Send email", ['exception' => $e]); return false; } return true; } /** * Get user emails list that can send email messages * @param string $uid * @return array */ public function getSenderAddressesFor(string $uid): array { $emails = []; $mailProviders = $this->mailManager->services($uid); foreach ($mailProviders as $mailServices) { $serviceEmails = array_filter(array_map(function ($service) { return $service->capable('MessageSend') ? $service->getPrimaryAddress()->getAddress(): null; }, $mailServices)); array_push($emails, ...$serviceEmails); } return $emails; } }