Skip to content

Instantly share code, notes, and snippets.

@romaricdrigon
Created August 29, 2013 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save romaricdrigon/9d05ea2e2ba752035257 to your computer and use it in GitHub Desktop.
Save romaricdrigon/9d05ea2e2ba752035257 to your computer and use it in GitHub Desktop.
TwigMailerService
{# Template than our mails can extends #}
{% block subject '' %}
{% block body_html '' %}
{% block body_text %}
{{ block('body_html')|striptags }}
{% endblock %}
parameters:
twig_mailer.sender_name: Admin
twig_mailer.sender_address: admin@noreply.com
<?php
$this->get('ns.twig_mailer')->send(
'NSWebBundle:Mail:forgottenPassword.mail.twig',
$user->getEmail(),
array('user' => $user, 'token' => $token)
);
{% extends 'NSWebBundle:Mail:_mail.html.twig' %}
{% block subject %}
Forgotten password
{% endblock %}
{% block body_html %}
Some random message
{% endblock %}
<!-- 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>
<?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