HowTo: Create and register a MailTypeBuilderPlugin

Edit on GitHub

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

All MailTransfers need to know which mail type has to be used for further internal processing. The mail type has to be a string.

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 MailTypeBuilderPlugin through the MailTransfer.

Let’s say you have a module named FooBar, where you want to add automated mail sending. To enable that feature, follow these steps:

Example of a FooBarMailTypeBuilderPlugin:

namespace Pyz\Zed\FooBar\Communication\Plugin\Mail;

use Generated\Shared\Transfer\MailRecipientTransfer;
use Generated\Shared\Transfer\MailTemplateTransfer;
use Generated\Shared\Transfer\MailTransfer;
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
use Spryker\Zed\MailExtension\Dependency\Plugin\MailTypeBuilderPluginInterface;

class FooBarMailTypeBuilderPlugin extends AbstractPlugin implements MailTypeBuilderPluginInterface
    protected const MAIL_TYPE = 'a name of the mail';

    protected const MAIL_TEMPLATE_HTML = 'FooBar/mail/your_mail.html.twig';

    protected const MAIL_TEMPLATE_TEXT = 'FooBar/mail/your_mail.text.twig';

    protected const GLOSSARY_KEY_MAIL_SUBJECT = 'foo_bar.mail.your_mail.subject';

    protected const PARAMETER_NAME = '%name%';

    public function getName(): string
        return static::MAIL_TYPE;

    public function build(MailTransfer $mailTransfer): MailTransfer
        return $mailTransfer
            ->setSubjectTranslationParameters([static::PARAMETER_NAME => $fooBarTransfer->getFooBarNameOrFail()])
                (new MailTemplateTransfer())
                (new MailTemplateTransfer())
                (new MailRecipientTransfer())

Register the FooBarMailTypeBuilderPlugin:

namespace Pyz\Zed\Mail;

use Pyz\Zed\FooBar\Communication\Plugin\Mail\FooBarMailTypeBuilderPlugin;
use Spryker\Zed\Mail\MailDependencyProvider as SprykerMailDependencyProvider;

class MailDependencyProvider extends SprykerMailDependencyProvider
    protected function getMailTypeBuilderPlugins(): array
        return [
            new FooBarMailTypeBuilderPlugin(),

In order to activate the mail functionality, follow these steps:

  1. To verify the mail provider is created and registered, go to How to create and register a mail provider.
  2. Create and adjust MailTransfer:
    $mailTransfer = new MailTransfer();
    $mailTransfer->setFooBar(new FooBarTransfer());

Call MailFacade::handleMail($mailTransfer).

If everything is set up properly the mail will be sent.

Follow Tutorial sending an email to get more information.