HowTo: Create and register a MailTypePlugin

Edit on GitHub

This document shows how to create and register a mail provider you want to use.

Create a MailTypePlugin

MailType is a class used to build the entire MailTransfer through an easy-to-use interface. Create the MailTypePlugin within the Mail module, which sends out the emails, and implement MailTypePluginInterface. Then, in the build() method, set up your email.

MailType is a class used to build the entire MailTransfer through an easy-to-use interface. Create the MailTypePlugin within the Mail module, which sends out the emails, and implement MailTypePluginInterface. Then, in the build() method, set up your email.

Within the build() method, you have access to the MailBuilderInterface, which makes it easy to enrich the MailTransfer with the information needed to send out the emails. You also have access to the MailTransfer itself through the MailBuilderInterface. This one, for example, is used to get the recipient information from a given transfer object.

In most cases, you can add a specific transfer to the MailTransfer—for example, a CustomerTransfer when a customer registers. This transfer object is available in your MailType through the MailTransfer.

Example of a MailTypePlugin:

<?php
namespace Pyz\Zed\YourBundle\Communication\Plugin\Mail;

use Spryker\Zed\Kernel\Communication\AbstractPlugin;
use Spryker\Zed\Mail\Business\Model\Mail\Builder\MailBuilderInterface;
use Spryker\Zed\Mail\Dependency\Plugin\MailTypePluginInterface;

class YourMailTypePlugin extends AbstractPlugin implements MailTypePluginInterface
{
    const MAIL_TYPE = 'name of your mail';

    /**
     * @return string
     */
    public function getName(): string
    {
        return static::MAIL_TYPE;
    }

    /**
     * @param \Spryker\Zed\Mail\Business\Model\Mail\Builder\MailBuilderInterface $mailBuilder
     *
     * @return void
     */
    public function build(MailBuilderInterface $mailBuilder): void
    {
        $this
            ->setSubject($mailBuilder)
            ->setHtmlTemplate($mailBuilder)
            ->setTextTemplate($mailBuilder)
            ->setSender($mailBuilder)
            ->setRecipient($mailBuilder);
    }

    ...

}

Register the MailTypePlugin

To add your MailType to the MailTypeCollection, add it to your MailDependencyProvider:

<?php
namespace Pyz\Zed\Mail;

...

public function provideBusinessLayerDependencies(Container $container)
{
    ...

    $container->extend(self::MAIL_TYPE_COLLECTION, function (MailTypeCollectionAddInterface $mailCollection) {
        $mailCollection->add(new YourMailTypePlugin());

        return $mailCollection;
    }

    ...
}
...