Skip to content

Instantly share code, notes, and snippets.

@MrMooky
Created June 29, 2020 12:40
Show Gist options
  • Save MrMooky/2654dee5578c66773ef5d673a4a8da7f to your computer and use it in GitHub Desktop.
Save MrMooky/2654dee5578c66773ef5d673a4a8da7f to your computer and use it in GitHub Desktop.
> Usage
$recipient = array('[email protected]');
$sender = array('[email protected]' => 'Account');
$subject = 'Hello there';
$templateName = "ThisIsTheExtbaseEmailTemplate"; // .html file
$content = array(
'firstname' => $postData['first_name']
);
$this->sendTemplateEmail($recipient, $sender, $subject, $templateName, $content);
---
/**
* @param array $recipient recipient of the email in the format array('[email protected]' => 'Recipient Name')
* @param array $sender sender of the email in the format array('[email protected]' => '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->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
$extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('siteway');
$templatePathAndFilename = $extensionPath . 'Resources/Private/Templates/Email/' . $templateName . '.html';
$layoutRootPath = $extensionPath . 'Resources/Private/Layouts';
$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);
$message->setBody($emailBody, 'text/html', 'utf-8');
$message->send();
return $message->isSent();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment