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:
Of course, this bundle and component are fully decoupled and can be used in any Symfony application.
Installation is pretty easy, thanks to Composer manager. To add bundle to your composer.json
, use following command:
$ composer require sylius/mailer-bundle
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(),
);
}
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
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.
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.
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 %}
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.
Most common ways to send e-mail are:
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.
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:
<?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.
}
}
services:
app.email_sender.adapter.custom:
class: AppMailerAdapterCustomAdapter
sylius_mailer:
adapter: app.email_sender.adapter.custom
And that’s all! Now e-mails will be sent using your custom adapter.
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!