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:
More from our blog
Technical 3 min read 04.12.2024
Here’s everything you had to know about the first major release since 2017! Over 7 years after the first major release, on Nov 12, 2024, we have released Sylius 2.0.0. We had a great opportunity to announce it first at SyliusCon in Lyon, but now, as we are back to… Read More
3 min read 22.11.2024
The emotions start to settle after SyliusCon, and it’s time to reflect on this incredible milestone in our journey. Why a milestone? Because SyliusCon exceeded our expectations in every possible way. We broke attendance records and brought together the key figures of our community, numerous partners, freelancers, and simply all… Read More
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
Comments