3 min. read
Categories:
Sending configurable e-mails in Symfony
Sending e-mails is a common task for most of web apps. We introduce you SyliusMailerBundle, which makes it easy and fun.
Sending configurable e-mails in Symfony

Every developer, during their adventure with PHP programming has been struggling with sending emails in a web application. However using PHP send() function is often insufficient for common web applications, when you need templates, variables, configurations etc.

Fortunately, Sylius provides SyliusMailerBundle and Mailer component, with some awesome features:

  • E-Mails configurable via YAML and Doctrine entities
  • Various adapters, including TwigSwiftMailer
  • Simple interface for sending e-mails with variables
  • Flexible system of events for sending and rendering e-mails

Of course, this bundle and component are fully decoupled and can be used in any Symfony application.

Installation and configuration

Installation is pretty easy, thanks to Composer manager. To add bundle to your composer.json, use following command:

$ composer require sylius/mailer-bundle

Adding required bundles to the kernel

To make SyliusMailerBundle work, you ought to add it and its dependencies to your kernel. You should also add SyliusResourceBundle, if you’re not using it.

<?php

public function registerBundles()
{
    $bundles = array(
        new FOSRestBundleFOSRestBundle(),
        new JMSSerializerBundleJMSSerializerBundle($this),
        new StofDoctrineExtensionsBundleStofDoctrineExtensionsBundle(),
        new WhiteOctoberPagerfantaBundleWhiteOctoberPagerfantaBundle(),

        new SyliusBundleMailerBundleSyliusMailerBundle(),
        new SyliusBundleResourceBundleSyliusResourceBundle(),

        // Other bundles...
        new DoctrineBundleDoctrineBundleDoctrineBundle(),
    );
}

Container configuration

Last step is adding some crucial directives to your container configuration.

sylius_mailer:
    driver: doctrine/orm
    sender:
        name: My website
        address: [email protected]

stof_doctrine_extensions:
    orm:
        default:
            timestampable: true

Updating database schema

php app/console doctrine:schema:update --force

Beware of using this command – it should be executed only in dev environment, while in production you should use Doctrine migrations system.

That’s it! SyliusMailerBundle is now fully configured and you can start using it in your application.

Sending emails

SyliusMailerBundle uses Swiftmailer library to send e-mail as default. However, you can implement your own adapters and use a different library.

Configuration of new e-mail sending is pretty easy and consist of few simple steps. Let’s say you want to send emails with newsletters to shop subscribers.

Template

First, we should create its template. As default, SyliusMailerBundle uses twig renderer, so we need to create proper .twig file and place it in a specific directory.

// AppBundle/Resources/views/Email/newsletter.html.twig

{% block body %}
    Hello {{ name }}!

    A new newsletter has been announced. Check this out <a href="{{ link }}">here</a>.
{% endblock %}

E-mail configuration

Every e-mail should be configured under sylius_mailer in your app/config/config.yml file.

sylius_mailer:
    driver: doctrine/orm
    sender:
        name: My website
        address: [email protected]
    emails:
        newsletter:
            subject: {{ month }} newsletter
            template: AppBundle/Resources/views/Email:newsletter.html.twig

You can also specify if template is enabled, as well as configure custom sender for specific mail.

sylius_mailer:
    driver: doctrine/orm
    sender:
        name: My website
        address: [email protected]
    emails:
        newsletter:
            subject: {{ month }} newsletter
            template: AppBundle/Resources/views/Email:newsletter.html.twig
            enabled: true/false
            sender:
                name: Custom sender
                address: [email protected]

Whole sylius_mailer configuration reference is available in SyliusMailerBundle documentation.

Sending

Most common ways to send e-mail are:

  • using controller action
  • using listener

Of course, you can use sender service anywhere (e.g. commands), as every usual service.

Service responsible for sending e-mails is registered as sylius.email_sender, so it can be easily retrieved in controller action. All you must do, is use send($mail, array $recipientsEmails, array $parameters) function.

<?php

namespace AppAppBundleController;

use SymfonyComponentHttpFoundationRequest;

class NewsletterController
{
    public function sendNewsletterAction(Request $request)
    {
        // Your code.

        $this->get('sylius.email_sender')->send('newsletter', array('[email protected]'), array('month' => $month, 'name' => $userName, 'link' => $newsletterLink));
    }
}

Of course, you can rely on event system and inject sylius.email_sender service to proper listener.

<?php

namespace AppAppBundleController;

use AppEventNewsletterCreatedEvent;
use SyliusComponentMailerSenderSenderInterface;

class NewsletterNotificationListener
{
    /**
     * @var SenderInterface
     */
    private $sender;

    /**
     * @param SenderInterface $sender
     */
    public function __construct(SenderInterface $sender)
    {
        $this->sender = $sender;
    }

    /**
     * @param NewsletterCreatedEvent $event
     * @param array                  $recipients
     */
    public function onNewsletterCreation(NewsletterCreatedEvent $event, array $recipients)
    {
        $month = $event->getNewsletter()->getMonth();
        $link = $event->getNewsletter()->getLink();
        $userName = $event->getUser()->getName();

        $this->sender->send('newsletter', $recipients, array('month' => $month, 'name' => $userName, 'link' => $link));
    }
}

And that’s all! Emails are send and you can be sure that your’s shop subscribers are going to receive them.

Custom adapter

Of course, using Swiftmailer is not the only way to send e-mail messages. You may want to use external API or implement some custom, amazing algorithm. Fortunately, you can do this thanks to adapter system implemented in SyliusMailerBundle. Moreover, writing and configuring custom adapter is really simple and requires less effort than you can imagine.

Three simple steps are:

Implement adapter

<?php

namespace AppMailerAdapter;

use SyliusComponentMailerSenderAdapterInterface;
use SyliusComponentMailerModelEmailInterface

class CustomAdapter implements AdapterInterface
{
    /**
     * @param EmailInterface $email
     * @param array          $recipients
     * @param array          $data
     */
    public function send(EmailInterface $email, array $recipients, array $data = array())
    {
        // Your custom logic.
    }
}

Register newly created adapter as service

services:
    app.email_sender.adapter.custom:
        class: AppMailerAdapterCustomAdapter

Configure it

sylius_mailer:
    adapter: app.email_sender.adapter.custom

And that’s all! Now e-mails will be sent using your custom adapter.

Summary

Sending e-mails in web application is one of the basic challenges that every developer must face. SyliusMailerBundle tries to make it easier. We hope that with this bundle sending e-mails will be annoying never again.

We’ll be grateful for any feedback. Please leave them in the comments below. Thanks!

Tags:
Share:
Mateusz Zalewski
Long-time Core Team member and the main Trainer in the Sylius company with the current focus on improving training programs to make them as fun and developing for the Sylius newcomers as possible. He's also hung up on enhancing Sylius' architecture and its plugins' system. Privately, a huge enthusiast of history and astronomy.
More from our blog
Cloud 3 min read 17.06.2024
We are thrilled to announce that we just signed a strategic partnership with Platform.sh, and as a result, we are extending our offer with Sylius Cloud powered by Platform.sh. Platform.sh is a modern Platform-as-a-Service (PaaS) solution that allows businesses to leverage the cloud environment without losing access to the code… Read More
Technical 3 min read 11.06.2024
Abstract 1.12 released in Q4 2022 1.13 on Apr 23rd, 2024 (a year later than we anticipated while releasing 1.12) 3859 commits 23 contributors A stabilized Sylius API powered by API Platform It’s been a long and bumpy road. Having it behind our backs was a highway that led Sylius… Read More
Business Ecosystem News 3 min read 06.06.2024
Welcome to the May summary! As an open-source eCommerce framework, Sylius continues to evolve with significant contributions from our vibrant community and valuable product updates. Apart from describing the technical changes, we will also quickly summarize the Sylius Technical Fundamentals & Sylius Polish Community Meetup and eCommerce Day Kaunas, as… Read More
Comments