fredtro / mailer-bundle
Symfony2 bundle for sending emails using twig templates.
Installs: 14
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^5.5.9 || ^7.0
- swiftmailer/swiftmailer: ^4.3 || ^5.0 || ^6.0
- symfony/twig-bundle: ^2.7 || ^3.0
- twig/twig: ^1.28 || ^2.0
Requires (Dev)
- phpunit/phpunit: ~4.8|~5.0
- symfony/phpunit-bridge: ^2.7 || ^3.0
This package is not auto-updated.
Last update: 2024-05-15 02:51:03 UTC
README
Provides mailer for sending emails using twig templates. Initially inspired by FOSUserBundle TwigSwiftMailer.
Installation
Step 1: Download the Bundle
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.
Step 2: Enable the Bundle
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(), ); // ... } // ... }
Step 3: Configuration
At least the from address needs to be configured. Name is optionally.
fredtro_mailer: from: address: foo@example.com name: Example Customer Service
Step 4: Sending Emails
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']); }
Additional features:
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).