agentsquidflaps/abstract-mailer

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

Symfony abstraction layer for mailing

dev-master 2018-04-14 19:42 UTC

This package is not auto-updated.

Last update: 2020-07-29 16:24:13 UTC


README

Maintainability codecov Build Status

A simple abstraction layer for symfony mailing. Ideal for switching mail providers simply, without having to refactor a lot of code. With the added benefit of twig and event integration.

To install simply run...

composer require agentsquidflaps/abstract-mailer

To begin, you'll want to then to then extend the abstract mailer...

 namespace App\Mail;
 
 use Agentsquidflaps\AbstractMailer\AbstractMailer;
 
 /**
  * Class Mailer
  * @package App\Mail
  */
 class Mailer extends AbstractMailer {
    // Any other code you desire...
 }

The only required setting is the Swift mailer transport you would like to use...

// config/services.yaml

// These are optional, but recommended...
parameters:
  mail:
    from:
      test@test.test: Test Account Holder Name
    to: test@test.com
    
    
...a few moments later...

services:
  mailer.transport:
    class: Swift_SendmailTransport
    public: true
  App\Mail\Mailer:
    public: true
    arguments:
      - '@mailer.transport'
      // Below are optional if you've got autowiring turned on...
      - '%mail%'
      - '@your-alternative-twig-environment'
      - '@your-alternative-event-dispatcher'

So, should your mail needs change, all you have to do is change the mailer.transport service to suit.

To use send mail, simply create your Swift_Message from the mailer object (abstract mailer extends this). For example...

$mailer
  // if not set will default to the mail.to parameter...
  ->setTo($toEmail)
  // if not set will default to the mail.from parameter...
  ->setFrom($fromEmail)
  ->setTemplate('mail/twig_mail_template.html.twig', ['pass_variables'=>$here])
  // setting mode to anything other than live, will do everything except send the email...
  ->setMode('test')
  ->setSubject('Super Cool Fun Email Subject!!!')
  ->send()
;

This only requirement is that you set a twig template but you send.

Did you notice the event dispatcher part before? Yes, you can listen/subscribe to events. Here's a subscriber example...

namespace App\EventSubscriber;

use App\Mail\Mailer;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class MailerSubscriber
 * @package App\EventSubscriber
 */
class MailerSubscriber implements EventSubscriberInterface
{

    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return [
            Mailer::EVENT_PRE_SEND => 'preSend',
            Mailer::EVENT_POST_SEND => 'postSend',
        ];
    }

    public function preSend()
    {
        $this->logger->info('This was sent from the mailer preSend event');
    }
    
    public function postSend()
    {
        $this->logger->info('This was sent from the mailer postSend event');
    }
}