SwiftMailer Events and Symfony — An Introduction

If you’re familiar with the Symfony framework and have built a few applications with it, you’ve likely been exposed to, if not made use of, Symfony’s event dispatcher and related components. And perhaps you’ve found yourself wanting to hook into events related to the sending of e-mails, which, if you’re like me, means using the SwiftMailer library, which comes bundled with the Symfony framework. But how exactly do you “hook into” those Swift Mailer events? And which events can you respond to?

Unfortunately, a glance at the documentation for both Symfony and Swift Mailer doesn’t immediately provide answers to these questions, so here are a few important things to understand if you’re just getting started with this area of SwiftMailer.

Creating an Event Listener

Swift Mailer has it’s own event dispatcher component, and you respond to those events by registering a plugin that has methods corresponding to events broadcast by SwiftMailer.

For example, the Reporter Plugin class, bundled with SwiftMailer and found in swiftmailer/lib/classes/Swift/Plugins/ReporterPlugin.php, has as it’s primary method “sendPerformed”:*

*Code edited and simplified for brevity.

The plugin notifies another class that a particular message sent to a given address was either sent successfully or failed. The important point to note, however, is that the method name corresponds to a dispatched event.

For example, inside the Swift_Transport_AbstractSmtpTransport class — one of the “transport” classes used to actually send mail — you’ll find the send() method which dispatches the following events:


Swift Mailer’s event dispatcher takes care of the rest, and calls the “beforeSendPerformed” and “sendPerformed” methods on registered plugins like the Reporter Plugin whenever the corresponding event is dispatched.

Creating your own plugin, then, is a simple as creating a class that implements an interface that extends the “Swift_Events_EventListener” interface, such as “Swift_Events_SendListener”, which will tell you what events the listener can listen to and what event object it will receive once a given event occurs; and then registering it with SwiftMailer.

You can find the various event types and listener interfaces in this sub-directory: swiftmailer/lib/classes/Swift/Events

Registering a Plugin

Once you’ve created your own plugin (i.e., event listener), you can register it with SwiftMailer by configuring your class as a service via Symfony’s service.yml file and adding this to the “tags:” portion of the definition:

Note that “default” can be replaced by the name of a particular SwiftMailer instance if you’ve created more than one, and if you happen to be creating your own Swift_Mailer object (e.g., you’re not using Symfony), you can call the registerPlugin() method on it directly as shown here.

Written by

Web Application Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store