initializeRouter(); } /** * Initializes the injected router-object. * * Manual initialization is required when building links in CLI mode. */ protected function initializeRouter() { // Note that setRoutesConfiguration() is not defined in the interface. if ($this->router instanceof Router || ObjectAccess::isPropertySettable($this->router, 'routesConfiguration')) { $routesConfiguration = $this->configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_ROUTES); // Calls the setter method, if possible. ObjectAccess::setProperty($this->router, 'routesConfiguration', $routesConfiguration); } } /** * @param Recipient $recipient recipient of the mail * @param string $subject subject of the mail * @param string $packageKey the name of the package which contains the template * @param string $templateIdentifier name of the mail template to use @see renderMailBody() * @param array $variables variables that will be available in the mail template. in the format array('' => '', ....) * @return boolean TRUE on success, otherwise FALSE */ public function sendTemplateBasedMail(Recipient $recipient, $subject, $packageKey, $templateIdentifier, array $variables = []) { $plaintextBody = $this->renderPlaintextMailBody($packageKey, $templateIdentifier, $variables); $htmlBody = $this->renderHtmlMailBody($packageKey, $templateIdentifier, $variables); $mail = $this->createMail($recipient, $subject, $plaintextBody, $htmlBody); return $this->sendMail($mail); } /** * @param string $packageKey * @param string $templateIdentifier * @param array $variables * @return string */ protected function renderPlaintextMailBody($packageKey, $templateIdentifier, array $variables) { return $this->renderMailBody($packageKey, $templateIdentifier, $variables, 'txt'); } /** * @param string $packageKey * @param string $templateIdentifier * @param array $variables * @return string */ protected function renderHtmlMailBody($packageKey, $templateIdentifier, array $variables) { return $this->renderMailBody($packageKey, $templateIdentifier, $variables, 'html'); } /** * @param string $packageKey * @param string $templateIdentifier * @param array $variables * @param string $format * @return string */ protected function renderMailBody($packageKey, $templateIdentifier, array $variables, $format) { $standaloneView = new StandaloneView(); $request = $standaloneView->getRequest(); $request->setControllerPackageKey($packageKey); $templateFilePath = 'resource://' . $packageKey . '/Private/Templates/Mail/' . $templateIdentifier . '.' . $format; $standaloneView->setTemplatePathAndFilename($templateFilePath); $standaloneView->assignMultiple($variables); return $standaloneView->render(); } /** * @param Recipient $recipient * @param string $subject * @param string $plaintextBody * @param string $htmlBody * @return Message */ protected function createMail(Recipient $recipient, $subject, $plaintextBody, $htmlBody) { $mail = new Message(); $mail->setFrom([ $this->settings['sender']['emailAddress'] => $this->settings['sender']['name'] ]) ->setTo($recipient->toArray()) ->setSubject($subject) ->setBody($plaintextBody); $mail->addPart($htmlBody, 'text/html'); return $mail; } /** * Sends the mail and creates a system logger entry on failure. * * @param Message $mail * @return boolean TRUE on success, otherwise FALSE */ protected function sendMail(Message $mail) { $numberOfRecipients = 0; $exception = NULL; try { $numberOfRecipients = $mail->send(); } catch (\Exception $e) { $exception = $e; } if ($numberOfRecipients === 0) { $this->logDeliveryFailure($mail, $exception); return FALSE; } return TRUE; } /** * Logs a delivery failure. * * @param Message $mail * @param \Exception|NULL $exception */ protected function logDeliveryFailure(Message $mail, \Exception $exception = NULL) { $additionalData = [ 'subject' => $mail->getSubject(), 'id' => (string) $mail->getHeaders()->get('Message-ID') ]; if ($exception !== NULL) { $additionalData['exception'] = '#' . $exception->getCode() . ': ' . $exception->getMessage(); } $this->systemLogger->log('Mail delivery failed.', LOG_ERR, $additionalData); } }