enl/swiftmailer-extras

There is no license information available for the latest version (dev-master) of this package.

Extra features for Swiftmailer - PHP mailing library

dev-master 2016-11-27 16:00 UTC

This package is auto-updated.

Last update: 2024-10-29 04:58:50 UTC


README

This library adds two facilities two Swiftmailer library:

  • Adapter to PSR-3 compatible logger;
  • Mailer class that can switch from messages spooling to realtime sending.

Logging

To use any kind of PSR-3 compatible logger with Swift you need to register plugin with logging adatper:

use Enl\Swiftmailer\Logger\PsrAdapter;

$logger = new Logger(); // Logger MUST implement \Psr\Log\LoggerInterface
$adapter = new PsrAdapter($logger);

$mailer = new Swift_Mailer();
$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($adapter));

Optional Spooling

As you know, to use Spool facility for Swift_Mailer, you need to define transport like this:

$transport = new Swift_Transport_SpoolTransport(new Swift_MemorySpool());
$mailer = new Swift_Mailer($transport);

It is absolutely OK until you need to send this email right now... To achieve this goal, I created a wrapper for swift mailer, which does the trick:

$realTransport = new Swift_Transport_NullTransport();
$spool = new Swift_MemorySpool();
$mailer = new Enl\Swifthmailer\Mailer($realTransport, $spool);

To send the message to a queue you should use mailer as usual:

$mailer->send(new Swift_Message());

To immediately send exactly one message you should use immediately function before sending:

$mailer->immediately()->send(new Swift_Message());
// Next message will be sent to a queue!
$mailer->send(new Swift_Message());