Snippets

This Mächler TYPO3 Exbase Controller - send mail using fluid template

Created by This Mächler last modified
	/**
	* sendVerificationMail
	*
	* @param string $subject the email subject
	* @param \MMC\MmcDirectmailSubscription\Domain\Model\Address $address 
	* @param string $bodyTemplateFile the template file for the mail body
	* @return void
	*/
	protected function sendVerificationMail( $subject, $address, $bodyTemplateFile ){
		// make mailer instance
		$mail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
		// setup email components
		$recipients = array( $address->getEmail() => $address->getName() );
		$from = array( $this->settings['fromEmail'] => $this->settings['fromName'] );
		$view = $this->getStandaloneView( $bodyTemplateFile );
		$view->assign( 'lang', $GLOBALS['TSFE']->lang );
		$view->assign( 'sysLangUid', $GLOBALS['TSFE']->sys_language_uid );
		$view->assign( 'address', $address );
		$mailBody = $view->render();
		// send mail
		try{
			$mail->setFrom( $from )
				->setTo( $recipients )
				->setSubject( $subject )
				->setBody($mailBody, 'text/html')
				->send();
		} catch ( \Exception $e ){
			$this->addFlashMessage('error while sending mail: '.$e->getMessage() );
		}
		if(! $mail->isSent())
			$this->addFlashMessage('error while sending mail: mail could not be sent' );		
	}
	
	/* ------------------------------------- Utility ---------------------------------------- */

	
	protected function getStandaloneView($file) {
	  // create another instance of Fluid
	  $renderer = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
	  //$renderer->setFormat("html");
	  $renderer->setControllerContext($this->controllerContext);
	   // find the view-settings and set the template-files
	  $conf = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
	  $renderer->setTemplatePathAndFilename(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['view']['templateRootPath']) . $file);
	  $renderer->setLayoutRootPath(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['view']['layoutRootPath']));
	  $renderer->setPartialRootPath(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($conf['view']['partialRootPath']));
	 
	  // and return the new Fluid instance
	  return $renderer;
	}
	

Comments (1)

  1. Linda Melson
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.