' => '') * @param array $recipient recipient of the email in the format array('' => '') * @param array $variables variables that will be available in the email template. in the format array('' => '', ....) * @return boolean TRUE on success, otherwise FALSE */ public function sendTemplateBasedEmail($templateIdentifier, $subject, array $sender, array $recipient, array $variables = array()) { $this->initializeRouter(); $plaintextBody = $this->renderEmailBody($templateIdentifier, 'txt', $variables); $htmlBody = $this->renderEmailBody($templateIdentifier, 'html', $variables); $mail = new \TYPO3\SwiftMailer\Message(); $mail ->setFrom(array($sender['email'] => $sender['name'])) ->setTo(array($recipient['email'] => $recipient['name'])) ->setSubject($subject) ->setBody($plaintextBody) ->addPart($htmlBody, 'text/html'); return $this->sendMail($mail); } /** * @param string $templateIdentifier * @param string $format * @param array $variables * @return string */ protected function renderEmailBody($templateIdentifier, $format, array $variables) { $standaloneView = new \TYPO3\Fluid\View\StandaloneView(); $request = $standaloneView->getRequest(); $request->setControllerPackageKey('My.Package'); $templatePathAndFilename = sprintf('resource://My.Package/Private/EmailTemplates/%s.%s', $templateIdentifier, $format); $standaloneView->setTemplatePathAndFilename($templatePathAndFilename); $standaloneView->assignMultiple($variables); return $standaloneView->render(); } /** * Sends a mail and creates a system logger entry if sending failed * * @param \TYPO3\SwiftMailer\Message $mail * @return boolean TRUE on success, otherwise FALSE */ protected function sendMail(\TYPO3\SwiftMailer\Message $mail) { $numberOfRecipients = 0; // ignore exceptions but log them $exceptionMessage = ''; try { $numberOfRecipients = $mail->send(); } catch (\Exception $e) { $exceptionMessage = $e->getMessage(); } if ($numberOfRecipients < 1) { $this->systemLogger->log('Could not sent notification email "' . $mail->getSubject() . '"', LOG_ERR, array( 'exception' => $exceptionMessage, 'message' => $mail->getSubject(), 'id' => (string)$mail->getHeaders()->get('Message-ID') )); return FALSE; } return TRUE; } /** * Initialize the injected router-object * * @return void */ protected function initializeRouter() { $routesConfiguration = $this->configurationManager->getConfiguration(\TYPO3\Flow\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES); $this->router->setRoutesConfiguration($routesConfiguration); } } ?>