mnavarrocarter / mailman-bundle
A message manager/storage for your SwiftMailer messages
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.0
- symfony/framework-bundle: ^4.0
- symfony/swiftmailer-bundle: ^3.2
README
A Mail Message Manager for SwiftMailer
Install
composer require mnavarrocarter/mailman-bundle
Then register the bundle in the container:
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array
// ...
new MNC\MailmanBundle\MNCMailmanBundle(),
);
// ...
}
// ...
}
Then, before composer finishes loading, include the config in your config.yml
// app/config/config.yml
//...
mnc_mailman:
from_name: 'Awesome App'
from_email: 'no-reply@awesomeapp.com'
Usage
This bundle provides a service that is capable of storing messages definitions, so you can fetch them from the service itself.
There are two ways of registering messages. You can do it fetching mnc_mailman.message_storage service or injecting it. However, that method is discouraged and doesn't give you the advantage of centralization.
It is better to create a class called, for example, AuthMessageRegistrar
, make it extend
AbstractRegistrar
and then register it as a service with the mailman.registrar
tag.
The class must implement the register method:
// src/AppBundle/MessageRegistrar/AuthMessages.php
<?php
namespace Option\MediaBridgeBundle\MessageRegistrar;
use MNC\MailmanBundle\Registrar\AbstractRegistrar;
use MNC\MailmanBundle\Storage\MessageStorageInterface;
/**
* Class AuthMessages
*/
class AuthMessages extends AbstractRegistrar
{
public function register(MessageStorageInterface $storage)
{
// To register a message, simply pass it the twig template and
the subject.
$storage->store('auth.registered', [
'template' => 'AppBundle:mail:auth:registered.html.twig',
'subject' => 'Welcome to our awesome site!',
]);
// You can also pass other stuff
$storage->store('auth.on_password_change', [
'template' => 'AppBundle:mail:auth:password_change.html.twig',
'subject' => 'Your password has been changed',
'data' => ['global' => 'available data'],
'headers' => ['somecustom' => 'Mail header']
]);
// You can also override the default from address and name for a specific
// message
$storage->store('auth.on_account_delete', [
'template' => 'AppBundle:mail:auth:delete_accout.html.twig',
'subject' => 'Your account has been deleted',
'fromName' => 'Your former app',
'formEmail' => 'you-can-reply-now@formerapp.com'
]);
}
}
Then, register the service.
// app/config/services.yml
services:
app.auth_registrar:
class: AppBundle\MessageRegistrar\AuthMessages
tags:
- { name: mailman.registrar }
All the email messages will be available to be fetched as instances of \Swift_Message.
// src/Controller/SecurityController.php
class SecurityController extends Controller
{
// ...
public function sendRegisteredEmail(User $user)
{
/** @var \Swift_Message $message */
$message = $this->get('mnc_mailman.message_storage')->fetch('auth.registered', [
// An array of data to pass to the twig template
'user' => $user
]);
$message->setTo($user->getEmail(), $user->getName());
$this->mailer->send($message);
}
}