Provides mailer for sending emails using twig templates. Initially inspired by FOSUserBundle TwigSwiftMailer.
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require fredtro/mailer-bundle "^1.0"
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Fredtro\MailerBundle\FredtroMailerBundle(),
);
// ...
}
// ...
}
At least the from address needs to be configured. Name is optionally.
fredtro_mailer:
from:
address: foo@example.com
name: Example Customer Service
Define a twig template:
{% block subject %}subject{% endblock %}
{% block text %}Hello {{username}}!{% endblock %}
{% block html %}<h1>Hello {{username}}!</h1>{% endblock %}
Send mail:
public function someAction(){
$mailer = $this->get('fredtro.mailer');
$mailer->send('template.twig', 'bar@example.com', ['username' => 'fred']);
}
Callback
For access the \Swift_Message created before sending, you can pass a callback (Instance of Closure). You can use this for e.g. adding attachments, set reply or anything else related to the message object.
public function someAction(){
$mailer = $this->get('fredtro.mailer');
$mailer->send('template.twig', 'bar@example.com', ['username' => 'fred'], function(\Swift_Message $message){
//do your modifications here
$message->setFrom(['somebodyelse@example.com']);
});
}
Events
Before sending email: Fredtro\MailerBundle\Event\MailerEvents::BEFORE_EMAIL_SENT
After sending email: Fredtro\MailerBundle\Event\MailerEvents::EMAIL_SENT
Both use the Generic Event class from Symfony. The EMAIL_SENT event additionally provides the attribute 'sent', containing the swift result (Mailer).