Skip to content

Instantly share code, notes, and snippets.

@pumatertion
Created July 10, 2013 14:13
Show Gist options
  • Save pumatertion/5966636 to your computer and use it in GitHub Desktop.
Save pumatertion/5966636 to your computer and use it in GitHub Desktop.
Mail Service
<?php
namespace BLEICKER\Mailer\Services;
/* *
* This script belongs to the FLOW3 package "BLEICKER.Mailer". *
* *
* */
use TYPO3\FLOW3\Annotations as FLOW3;
use TYPO3\FLOW3\Error\Message;
/**
* @FLOW3\Scope("singleton")
*/
class Mailer {
/**
* @var \TYPO3\FLOW3\Security\Context
* @FLOW3\Inject
*/
protected $securityContext;
/**
* @FLOW3\Inject
* @var \BLEICKER\FLOW3\Security\Authentication\HashFactory
*/
protected $hashFactory;
/**
* @var \TYPO3\Fluid\View\StandaloneView
* @FLOW3\Inject
*/
protected $view;
/**
* @FLOW3\Inject
* @var \TYPO3\FLOW3\Object\ObjectManagerInterface
*/
protected $objectManager;
/**
* @var \TYPO3\SwiftMailer\MailerInterface
* @FLOW3\Inject
*/
protected $mailer;
/**
* Sending an Email to the Primary Electronic Email Address of an account if it is unproved
* @param \TYPO3\FLOW3\Mvc\Controller\ControllerContext $controllerContext
* @param \TYPO3\FLOW3\Security\Account $account
* @param array $arguments
*/
public function sendEmailApproving(\TYPO3\FLOW3\Mvc\Controller\ControllerContext $controllerContext, \TYPO3\FLOW3\Security\Account $account, array $arguments){
$hash = $this->objectManager->get('\BLEICKER\FLOW3\Security\Authentication\ElectronicAddressHash');
$hash->setElectronicAddress($account->getParty()->getPrimaryElectronicAddress());
$this->hashFactory->buildHashAndPersist($account->getParty()->getPrimaryElectronicAddress(), $hash);
$this->view->setControllerContext($controllerContext);
$templateRoot = 'resource://' . $controllerContext->getRequest()->getControllerPackageKey() ;
$actionName = $controllerContext->getRequest()->getControllerActionName();
$subpackageKey = $controllerContext->getRequest()->getControllerSubpackageKey();
$controllerName = $controllerContext->getRequest()->getControllerName();
$templatePath = $templateRoot . '/Private/Templates/'. $subpackageKey . '/'. $controllerName . '/SendEmailApproving';
$layoutPath = $templateRoot . '/Private/Layouts';
$this->view->setLayoutRootPath($layoutPath);
$this->view->setTemplatePathAndFilename($templatePath);
$message = $this->mailer->createMessage();
$message->setSubject($arguments['subject'])
->setFrom(array($arguments['fromEmail'] => $arguments['fromName']))
->setTo(
array(
$account->getParty()->getPrimaryElectronicAddress()->getIdentifier(),
$account->getParty()->getPrimaryElectronicAddress()->getIdentifier() => $account->getParty()->getName()->getFullName()
)
);
$this->view->assign('credentialSource', $hash->getCredentialsSource());
$this->view->setTemplatePathAndFilename($templatePath . '.html');
$message->addPart($this->view->render(), 'text/html');
$this->view->setTemplatePathAndFilename($templatePath . '.txt');
$message->addPart($this->view->render(), 'text/plain');
$this->mailer->send($message);
$controllerContext->getFlashMessageContainer()->addMessage(new Message('We send you an E-Mail. Please approve your E-Mail-Address by clicking on the link inside.', 1335999877, array(), 'Apprope your E-Mail-Address'));
}
/**
* Sending an Email to the Primary Electronic Email Address of an account if it is unproved
* @param \TYPO3\FLOW3\Mvc\Controller\ControllerContext $controllerContext
* @param \TYPO3\FLOW3\Security\Account $account
* @param array $arguments
*/
public function sendAccountTokenMail(\TYPO3\FLOW3\Mvc\Controller\ControllerContext $controllerContext, \TYPO3\FLOW3\Security\Account $account, array $arguments){
$hash = $this->objectManager->get('\BLEICKER\FLOW3\Security\Authentication\AccountHash');
$hash->setAccount($account);
$this->hashFactory->buildHashAndPersist($account, $hash);
$this->view->setControllerContext($controllerContext);
$templateRoot = 'resource://' . $controllerContext->getRequest()->getControllerPackageKey() ;
$actionName = $controllerContext->getRequest()->getControllerActionName();
$subpackageKey = $controllerContext->getRequest()->getControllerSubpackageKey();
$controllerName = $controllerContext->getRequest()->getControllerName();
$templatePath = $templateRoot . '/Private/Templates/'. $subpackageKey . '/'. $controllerName . '/SendAccountToken';
$layoutPath = $templateRoot . '/Private/Layouts';
$this->view->setLayoutRootPath($layoutPath);
$this->view->setTemplatePathAndFilename($templatePath);
$message = $this->mailer->createMessage();
$message->setSubject($arguments['subject'])
->setFrom(array($arguments['fromEmail'] => $arguments['fromName']))
->setTo(
array(
$account->getParty()->getPrimaryElectronicAddress()->getIdentifier(),
$account->getParty()->getPrimaryElectronicAddress()->getIdentifier() => $account->getParty()->getName()->getFullName()
)
);
$this->view->assign('credentialSource', $hash->getCredentialsSource());
$this->view->setTemplatePathAndFilename($templatePath . '.html');
$message->addPart($this->view->render(), 'text/html');
$this->view->setTemplatePathAndFilename($templatePath . '.txt');
$message->addPart($this->view->render(), 'text/plain');
$this->mailer->send($message);
$controllerContext->getFlashMessageContainer()->addMessage(new Message('We send you an E-Mail. Please click on the link inside to set a new password.', 1335999888, array(), 'Your Password'));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment