Created
August 7, 2014 07:11
-
-
Save smichaelsen/6146b773f63a0159aeb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Smichaelsen\Gist\Mail; | |
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext; | |
/** | |
* This class represents an email which can be rendered with fluid. | |
* You can use all the possibilities of TYPO3's MailMessage but instead of | |
* using ->setBody() and ->send() you rather use ->render($template) to | |
* render and send a fluid template. | |
* Before rendering you can ->setControllerContext() and ->assign() variables. | |
*/ | |
class FluidMailMessage extends \TYPO3\CMS\Core\Mail\MailMessage { | |
/** | |
* @var \TYPO3\CMS\Extbase\Mvc\Controller\ControllerContext | |
*/ | |
protected $controllerContext; | |
/** | |
* @var \TYPO3\CMS\Fluid\View\StandaloneView | |
* @inject | |
*/ | |
protected $view; | |
/** | |
* @param ControllerContext $controllerContext | |
*/ | |
public function setControllerContext(ControllerContext $controllerContext) { | |
$this->controllerContext = $controllerContext; | |
$this->view->setControllerContext($controllerContext); | |
} | |
/** | |
* Assigns a key value pair to the fluid template | |
* | |
* @param string $key | |
* @param mixed $value | |
*/ | |
public function assign($key, $value) { | |
$this->view->assign($key, $value); | |
} | |
/** | |
* See ->setTemplate() for possibilities to express the template path | |
* | |
* @param string $template | |
*/ | |
public function render($template) { | |
$this->setTemplate($template); | |
$content = $this->view->render(); | |
$this->setBody($content); | |
$this->send(); | |
} | |
/** | |
* You can either provide a absolute template path or a template path | |
* beginning with 'EXT:'. | |
* If you have provided a ControllerContext (->setControllerContext()) you | |
* can also just provide the file name when your template lies in | |
* EXT:yourext/Resources/Private/Templates/Mail/ | |
* And finally you can also provide the full template source as a string. | |
* | |
* @param string $template | |
*/ | |
protected function setTemplate($template) { | |
$possibleFullTemplatePaths = array( | |
GeneralUtility::getFileAbsFileName($template), | |
$template, | |
); | |
if ($this->controllerContext instanceof ControllerContext) { | |
$possibleFullTemplatePaths[] = ExtensionManagementUtility::extPath($this->controllerContext->getRequest()->getControllerExtensionKey()) . 'Resources/Private/Templates/Mail/' . $template . '.html'; | |
$possibleFullTemplatePaths[] = ExtensionManagementUtility::extPath($this->controllerContext->getRequest()->getControllerExtensionKey()) . 'Resources/Private/Templates/Mail/' . $template; | |
} | |
$templateIsFile = FALSE; | |
foreach ($possibleFullTemplatePaths as $possibleFullTemplatePath) { | |
if (file_exists($possibleFullTemplatePath)) { | |
$this->view->setTemplatePathAndFilename($possibleFullTemplatePath); | |
$templateIsFile = TRUE; | |
break; | |
} | |
} | |
if (!$templateIsFile) { | |
$this->view->setTemplateSource($template); | |
} | |
} | |
} |
This class is now available as composer package: smichaelsen/typo3-fluidmailmessage
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wouldn't call ->send() within render(), I feel like it's not in it's responsibility... Other than that this is great!