Skip to content

Instantly share code, notes, and snippets.

@smichaelsen
Created August 7, 2014 07:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save smichaelsen/6146b773f63a0159aeb3 to your computer and use it in GitHub Desktop.
Save smichaelsen/6146b773f63a0159aeb3 to your computer and use it in GitHub Desktop.
<?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);
}
}
}
@irnnr
Copy link

irnnr commented Aug 7, 2014

I wouldn't call ->send() within render(), I feel like it's not in it's responsibility... Other than that this is great!

@smichaelsen
Copy link
Author

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