Created
April 11, 2013 13:24
-
-
Save etobi/5363337 to your computer and use it in GitHub Desktop.
extbase mailservice
This file contains 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 | |
class Tx_SoEvents_Service_MailService { | |
/** | |
* @var Tx_Extbase_Object_ObjectManager | |
*/ | |
protected $objectManager; | |
/** | |
* @var Tx_Extbase_Configuration_ConfigurationManagerInterface | |
*/ | |
protected $configurationManager; | |
/** | |
* @var array | |
*/ | |
protected $settings; | |
/** | |
* @var string | |
*/ | |
protected $extensionName = 'SoEvents'; | |
/** | |
* @var string | |
*/ | |
protected $templateFormats = array( | |
'html' => 'text/html', | |
'txt' => 'text/plain' | |
); | |
/** | |
* @return void | |
*/ | |
public function initializeObject() { | |
$configuration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK); | |
$this->settings = $configuration['settings']['mailService']; | |
} | |
/** | |
* @param string $recipient | |
* @param string $templateName | |
* @param array $variables | |
* @throws Exception | |
* @return void | |
*/ | |
public function sendTo($recipient, $templateName, $variables) { | |
$settings = $this->settings; | |
if (is_array($this->settings[$templateName])) { | |
$settings = t3lib_div::array_merge_recursive_overrule($settings, $this->settings[$templateName]); | |
} | |
if (empty($recipient)) throw new Exception('No or invalid recipient', 1320422721); | |
if (empty($settings['sender'])) throw new Exception('No or invalid sender', 1320422722); | |
$recipient = array($recipient); | |
$sender = array($settings['sender']); | |
$message = $this->renderMessage($templateName, $variables) | |
->setTo($recipient) | |
->setFrom($sender); | |
if (!empty($settings['bcc'])) { | |
$message->addBcc($settings['bcc']); | |
} | |
$message->send(); | |
if (!$message->isSent()) { | |
throw new Exception('Error while sending mail', 1320415855); | |
} | |
} | |
/** | |
* @param string $templateName | |
* @param array $variables | |
* @return t3lib_mail_Message | |
*/ | |
protected function renderMessage($templateName, $variables) { | |
/** @var $message t3lib_mail_Message */ | |
$message = t3lib_div::makeInstance('t3lib_mail_Message'); | |
$templateRootPath = t3lib_div::getFileAbsFileName($this->settings['templateRootPath']); | |
$templateName = ucfirst($templateName); | |
$firstPart = TRUE; | |
foreach($this->templateFormats as $templateFormat => $contentType) { | |
$templateFile = $templateRootPath . $templateName . '.' . $templateFormat; | |
if (file_exists($templateFile)) { | |
$emailBody = $this->renderMessageBody($templateFormat, $templateFile, $variables); | |
if (empty($emailBody)) throw new Exception('Empty mail', 1320422723); | |
// embed images | |
if ($contentType === 'text/html') { | |
preg_match_all('/src=["\']([^"\']*)["\']/', $emailBody, $srcs); | |
foreach($srcs[1] as $src) { | |
$cid = $message->embed(Swift_Image::fromPath(PATH_site . $src)); | |
$emailBody = str_replace($src, $cid, $emailBody); | |
} | |
} | |
if ($firstPart) { | |
list($subject, $emailBody) = t3lib_div::trimExplode("\n", $emailBody, FALSE, 2); | |
$message->setBody(trim($emailBody), $contentType); | |
$firstPart = FALSE; | |
} else { | |
$message->addPart(trim($emailBody), $contentType); | |
} | |
} | |
} | |
if ($firstPart) throw new Exception('No Email template found', 1328093150); | |
return $message->setSubject($subject); | |
} | |
/** | |
* @return string | |
*/ | |
protected function renderMessageBody($format, $templateFile, $variables) { | |
/** @var $emailView Tx_Fluid_View_StandaloneView */ | |
$emailView = $this->objectManager->create('Tx_Fluid_View_StandaloneView'); | |
$emailView->setFormat($format); | |
$emailView->setTemplatePathAndFilename($templateFile); | |
$emailView->assignMultiple($variables); | |
$emailView->getRequest()->setControllerExtensionName($this->extensionName); | |
return $emailView->render(); | |
} | |
/** | |
* @param Tx_Extbase_Object_ObjectManager $objectManager | |
*/ | |
public function injectObjectManager(Tx_Extbase_Object_ObjectManager $objectManager) { | |
$this->objectManager = $objectManager; | |
} | |
/** | |
* @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager | |
*/ | |
public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) { | |
$this->configurationManager = $configurationManager; | |
} | |
/** | |
* @param string $extensionName | |
*/ | |
public function setExtensionName($extensionName) { | |
$this->extensionName = $extensionName; | |
} | |
/** | |
* @return string | |
*/ | |
public function getExtensionName() { | |
return $this->extensionName; | |
} | |
/** | |
* @param string $templateFormat | |
*/ | |
public function setTemplateFormat($templateFormat) { | |
$this->templateFormat = $templateFormat; | |
} | |
/** | |
* @return string | |
*/ | |
public function getTemplateFormat() { | |
return $this->templateFormat; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment