-
-
Save romaricdrigon/9d05ea2e2ba752035257 to your computer and use it in GitHub Desktop.
TwigMailerService
This file contains hidden or 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
{# Template than our mails can extends #} | |
{% block subject '' %} | |
{% block body_html '' %} | |
{% block body_text %} | |
{{ block('body_html')|striptags }} | |
{% endblock %} | |
This file contains hidden or 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
parameters: | |
twig_mailer.sender_name: Admin | |
twig_mailer.sender_address: [email protected] |
This file contains hidden or 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 | |
$this->get('ns.twig_mailer')->send( | |
'NSWebBundle:Mail:forgottenPassword.mail.twig', | |
$user->getEmail(), | |
array('user' => $user, 'token' => $token) | |
); |
This file contains hidden or 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
{% extends 'NSWebBundle:Mail:_mail.html.twig' %} | |
{% block subject %} | |
Forgotten password | |
{% endblock %} | |
{% block body_html %} | |
Some random message | |
{% endblock %} |
This file contains hidden or 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
<!-- from <services> --> | |
<service id="ns.twig_mailer" class="NS\Bundle\CoreBundle\Service\TwigMailer"> | |
<argument type="service" id="twig" /> | |
<argument type="service" id="mailer" /> | |
<argument type="string">%twig_mailer.sender_address%</argument> | |
<argument type="string">%twig_mailer.sender_name%</argument> | |
</service> |
This file contains hidden or 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 | |
namespace NS\Bundle\CoreBundle\Service; | |
use Twig_Environment; | |
use Swift_Message; | |
use Swift_Mailer; | |
class TwigMailer | |
{ | |
/** | |
* @var Twig_Environment | |
*/ | |
protected $twig; | |
/** | |
* @var Swift_Mailer | |
*/ | |
protected $mailer; | |
public function __construct(Twig_Environment $twig, Swift_Mailer $mailer, $senderEmail, $senderName) | |
{ | |
$this->twig = $twig; | |
$this->mailer = $mailer; | |
$this->fromEmail = $senderEmail; | |
$this->fromName = $senderName; | |
} | |
/** | |
* @param string $templateName full name of template to fetch, can contain 'subject', 'body_html', 'body_text' | |
* @param array $parameters to pss to Twig template | |
* @return Swift_Message | |
* @throw \InvalidArgumentException if Twig template is not found | |
*/ | |
public function getMessage($templateName, $parameters = array()) | |
{ | |
$template = $this->twig->loadTemplate($templateName); | |
$subject = $template->renderBlock('subject', $parameters); | |
$bodyHtml = $template->renderBlock('body_html', $parameters); | |
$bodyText = $template->renderBlock('body_text', $parameters); | |
if (!$subject) { | |
throw new \InvalidArgumentException('Provided template must have a subject block'); | |
} | |
if (!$bodyHtml) { | |
throw new \InvalidArgumentException('Provided template must have a body_html block'); | |
} | |
if (!$bodyText) { | |
throw new \InvalidArgumentException('Provided template must have a body_text block'); | |
} | |
$message = Swift_Message::newInstance() | |
->setSubject($subject) | |
->setFrom( | |
$this->fromEmail, | |
$this->fromName | |
) | |
->setBody($bodyText, 'text/plain') | |
->addPart($bodyHtml, 'text/html') | |
; | |
return $message; | |
} | |
/** | |
* @param string $templateName full name of template to fetch, can contain 'subject', 'body_html', 'body_text' | |
* @param string|array $addresses email address(es) to send to | |
* @param array $parameters to pass to Twig template | |
* @return int number of successfully send messages | |
* @throw \InvalidArgumentException if Twig template is not found | |
*/ | |
public function send($templateName, $addresses, $parameters = array()) | |
{ | |
$message = $this->getMessage($templateName, $parameters); | |
$message->setTo($addresses); | |
return $this->mailer->send($message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment