菜单

How to use the Fluid Standalone view to render template based emails

2015年02月2日 - extensions
$this->view->setTemplatePathAndFilename(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('donate') . 'Resources/Private/Templates/Donors/List_backend.html');

This feature is part of Extbase 1.3, included in TYPO3 4.5 LTS.

Find the Code for TYPO3 version >= 6.0 below

Add the following function to your controller (or preferably to a common abstract base controller).


/**
* @param array $recipient recipient of the email in the format array('recipient@domain.tld' => 'Recipient Name')
* @param array $sender sender of the email in the format array('sender@domain.tld' => 'Sender Name')
* @param string $subject subject of the email
* @param string $templateName template name (UpperCamelCase)
* @param array $variables variables to be passed to the Fluid view
* @return boolean TRUE on success, otherwise false
*/
protected function sendTemplateEmail(array $recipient, array $sender, $subject, $templateName, array $variables = array()) {
$emailView = $this->objectManager->create('Tx_Fluid_View_StandaloneView');
$emailView->setFormat('html');
$extbaseFrameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
$templateRootPath = t3lib_div::getFileAbsFileName($extbaseFrameworkConfiguration['view']['templateRootPath']);
$templatePathAndFilename = $templateRootPath . 'Email/' . $templateName . '.html';
$emailView->setTemplatePathAndFilename($templatePathAndFilename);
$emailView->assignMultiple($variables);
$emailBody = $emailView->render();

$message = t3lib_div::makeInstance('t3lib_mail_Message');
$message->setTo($recipient)
->setFrom($sender)
->setSubject($subject);

// Possible attachments here
//foreach ($attachments as $attachment) {
// $message->attach($attachment);
//}

// Plain text example
$message->setBody($emailBody, 'text/plain');

// HTML Email
#$message->setBody($emailBody, 'text/html');

$message->send();
return $message->isSent();
}

Usage:

PHP script:

$this->sendTemplateEmail(array('recipient@domain.tld' => 'Recipient Name'), array('sender@domain.tld' => 'Sender Name'), 'Email Subject', 'TemplateName', array('someVariable' => 'Foo Bar'));
In this example, the Email templates are expected in Templates/Email/[TemplateName].html. But this can be changed by modifying line 14.

Localized emails

If you want to use translated content in your email template, you’ll have to pass the current extension name to the standalone view like to make the f:translate view helper work as expected:

PHP script:

$extensionName = $this->request->getControllerExtensionName();
$emailView->getRequest()->setControllerExtensionName($extensionName);
Alternatively you can specify the complete path to your locallang file in the fluid template:

Update:
if you call another Action with $this->forward($nextActionName) after sending the Email you need replace in line 10:

PHP script:

$emailView = $this->objectManager->create('Tx_Fluid_View_StandaloneView', $this->configurationManager->getContentObject());

instead of:
PHP script:

$emailView = $this->objectManager->create('Tx_Fluid_View_StandaloneView');

Usage in TYPO3 6.0 and higher

Due to the namespacing and the removal of some code deprecated earlier, some slight changes appear on the 6.x Code. The functionality of the snippet has not changed.

PHP script:

/**
* @param array $recipient recipient of the email in the format array('recipient@domain.tld' => 'Recipient Name')
* @param array $sender sender of the email in the format array('sender@domain.tld' => 'Sender Name')
* @param string $subject subject of the email
* @param string $templateName template name (UpperCamelCase)
* @param array $variables variables to be passed to the Fluid view
* @return boolean TRUE on success, otherwise false
*/
protected function sendTemplateEmail(array $recipient, array $sender, $subject, $templateName, array $variables = array()) {
/** @var \TYPO3\CMS\Fluid\View\StandaloneView $emailView */
$emailView = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');

$extbaseFrameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
$templateRootPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($extbaseFrameworkConfiguration['view']['templateRootPath']);
$templatePathAndFilename = $templateRootPath . 'Email/' . $templateName . '.html';
$emailView->setTemplatePathAndFilename($templatePathAndFilename);
$emailView->assignMultiple($variables);
$emailBody = $emailView->render();

/** @var $message \TYPO3\CMS\Core\Mail\MailMessage */
$message = $this->objectManager->get('TYPO3\\CMS\\Core\\Mail\\MailMessage');
$message->setTo($recipient)
->setFrom($sender)
->setSubject($subject);

// Possible attachments here
//foreach ($attachments as $attachment) {
// $message->attach(\Swift_Attachment::fromPath($attachment));
//}

// Plain text example
$message->setBody($emailBody, 'text/plain');

// HTML Email
#$message->setBody($emailBody, 'text/html');

$message->send();
return $message->isSent();
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注