mnavarrocarter/mailman-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

A message manager/storage for your SwiftMailer messages

1.0.1 2018-04-03 18:40 UTC

This package is auto-updated.

Last update: 2019-08-18 18:31:21 UTC


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);
    }
}