PHP Mail Library
Component for sending emails and attachments.

Mailer Component

The Mailer component enables you to send emails using different supported protocols.

Install the component

The best way to install the component is using Composer.

composer require webiny/mailer

For additional versions of the package, visit the Packagist page.


Current supported protocols are:

To use the component, you first need to configuration set inside the component config file. If you open the ExampleConfig.yaml you can see two example configuration sets, Demo and Gmail. Follow this example to create your own set.

Here is an example configuration:

            CharacterSet: utf-8
            MaxLineLength: 78
            Priority: 2
                Name: Nikola Tesla
                Type: smtp
                Port: 465
                Password: ***
                Encryption: ssl
                AuthMode: login
                Threshold: 99
                Sleep: 1
            DisableDelivery: false
            Mode: template # template or html
            ApiKey: yourApiKey
            DisableDelivery: false
            Message:    # these are all optional
                FromEmail: ''
                FromName: ''
                Headers: []
                Important: false
                TrackOpens: null
                TrackClicks: null
                AutoText: null
                AutoHtml: null
                InlineCss: null
                UrlStripQs: null
                PreserveRecipients: null
                ViewContentLink: null
                BccAddress: ''
                TrackingDomain: null
                SigningDomain: null
                ReturnPathDomain: null
                Merge: true
                MergeLanguage: mailchimp
                Tags: []
                Subaccount: null
                GoogleAnalyticsDomains: []
                GoogleAnalyticsCampaign: ''
                Metadata: []
                RecipientMetadata: []
                Attachments: []
            ApiUser: yourApiUser
            ApiKey: yourApiKey
            DisableDelivery: false
                Wrapper: ['*|', '|*']

You can have unlimited configuration sets.

To register the config with the component, just call Mailer::setConfig($pathToYamlConfig).

Depending on defined Transport.Type other transport parameters are required.

Configuration parameters

The Mailer configuration consists of several parameters that are explained in the next few sections.

Note: Some of the configuration parameters are bridge-specific, like the AntiFlood parameter. The default bridge is the SwiftMailer, which of course supports the AntiFlood measures.

Character set (CharacterSet)

This is the default character set that will be used in encoding your email content. By default the character set is set to utf-8 which supports most language characters. You might need to change this for some languages, for example, like Japanese.

Sender (Sender)

This is the default sender that will be set on your outgoing emails.

Transport (Transport)

The transport configuration block consists of following parameters:

  • Type
    • defines the type of the connection
    • can be smtp, mail or sendmail

These parameters are needed only in case of a SMTP connection:

  • Host
    • defines the location of your smtp host
  • Port
    • the port used to connect to the host
    • port can vary based on the defined encryption and auth_mode
  • Username
    • username needed to connect to the host
  • Password
    • password needed to connect to the host
  • Encryption
    • encryption used for the connection
    • this parameter is optional
    • can be ssl or tls based on your host
  • AuthMode
    • authorization mode used to connect to the host
    • this parameter is optional
    • can be plain, login, cram-md5, or null

AntiFlood (AntiFood)

Some mail servers have a set of safety measures that limit the amout of emails that you can send per connection or in some time interval. This is mostly to discourage spammers to user their services, but sometimes that might cause a problem even for non-spammers. In order to avoid falling into these safety measure the AntiFood parameter can limit how many emails you can send per connection and how much time you have to wait until you can establishe a new connection.

Don't worry about disconnecting, connecting again and resuming the sending of emails...this is all fully authomized and you don't have to do anything.

The AntiFood param consists of two attributes:

  • Threshold
    • defines how many emails to send per one connection
  • Sleep
    • defines how many seconds to wait until a new connection can be established and the sending resumed


Using the Mailer component is quite simple, just implement the MailerTrait, build your message and send it.

Here is one simple usage example:

class MyClass
    use \Webiny\Component\Mailer\Bridge\MailerTrait;

    function sendEmail() {
        // get the Mailer instance
        $mailer = $this->mailer('Default');

        // let's build our message
        $msg = $mailer->getMessage();
        $msg->setSubject('Hello email')
            ->setBody('This is my test email body')
            ->setTo(new Email('', 'Jack'));

        // send it

Now if you have multiple senders, and let's say you want to send all of them the same email, but just with a little difference, for example that in each email you put the name of the specific user.

class MyClass
    use \Webiny\Component\Mailer\Bridge\MailerTrait;

    function sendEmail() {
        // get the Mailer instance
        $mailer = $this->mailer('Default');

        // let's build our message
        $msg = $mailer->getMessage();
        $msg->setSubject('Hello email')
             ->setBody('Hi {name},
                           This is your new password: <strong>{password}</strong>.')
                    new Email(''),
                    new Email('')

        // before sending, let's define the decorator replacements
        $replacements = [
            '' => [
                'name'     => 'Jack',
                'password' => 'seCre!'

            '' => [
                'name'     => 'Sara',
                'password' => 'Log!n'

        // send it


The default bridge library is SwiftMailer (

If you wish to create your own driver ,you need to create three classes:

  • Message
    • this class should implement \Webiny\Component\Mailer\Bridge\MessageInterface
    • this class is used for populating message attributes, like sender, body ...
  • Transport
    • this class should implement \Webiny\Component\Mailer\Bridge\TransportInterface
    • it is used for sending the message
  • Mailer
    • this class should implement \Webiny\Component\Mailer\Bridge\MailerInterface
    • this class has only two methods, one returns an instance of Message and the other an instance of Transport


To run unit tests, you need to use the following command:

$ cd path/to/Webiny/Component/Mailer/
$ composer.phar install
$ phpunit

To learn what you can do with Webiny Framework, checkout Webiny Platform - open-source content management system.

Explore Webiny Framework components:

  • Amazon currently supports implementation of Amazon S3
  • Annotations component for parsing annotations from a class, method or a property
  • Cache provides several caching libraries like Apc, Couchbase, Memcache and Redis
  • ClassLoader a PSR-0, PSR-4 and PEAR class loader
  • Config a very handy library for parsing YAML, INI, JSON and PHP configuration files
  • Crypt library for encoding, decoding and validating hashes
  • Entity an ODM layer (currently only supports MongoDB)
  • EventManager want to do event-based development, this is a library for you
  • Http library for parsing HTTP requests and creating HTTP responses
  • Image library for image manipulation
  • Logger PSR-3 log components for logging code execution in runtime
  • Mailer component for sending emails
  • Mongo MongoDB class wrapper
  • OAuth2 library for working with OAuth2, currently supports Facebook, LinkedIn and Google+
  • REST fully featured REST library with caching, security and rate control
  • Router handles defining, parsing, creating and matching url routes
  • Security provides authorization and authentication layer
  • ServiceManager want to write truly service based, loosely-coupled code, this library provides that
  • StdLib this component provides object wrappers for Arrays, Strings, Urls and DateTime data types
  • Storage storage abstraction layer that simplifies the way you work with files and directories
  • TemplateEngine provides a layer for rendering view templates and defining template plugins and manipulators
  • TwitterOAuth library for working with Twitter API using Twitter OAuth


Lastest from our blog:

The Power of the Community

In the focus of recent feedback that I got, I think there is a need for me to justify some of the decisions we made while developing Webiny Framework.

Share & subscribe: