Symfony – testing emails in the browser

Blog Symfony
TAGS: developers

Symfony – testing emails in the browser


System is sending over 20 different email notifications. Will be nice to have link for quick emails preview.
Create controller that will use renderView to display all email templates on one page.

Symfony 4.4, popular PHP framework, can be configured to use Swift Mailer library. Configuration can be defined in swiftmailer.yaml file: sending using own SMTP server or other email providers like: SendGrid, Amazon SES or Gmail. For Advanced User Management – FOSUserBundle can be used. It provides user registration and password recovery, including “confirmation per email” functionality.

Testing emails

Testing email templates rendering can be tricky. Often system notifications are triggered on special conditions / time intervals. In those situations tests can be time-consuming or not thorough enough.

Sending to a Specified Email Address

You can force symfony to send all notifications to your e-mail.

# config/packages/dev/swiftmailer.yaml
    delivery_addresses: ['[email protected]']

Email Testing Tools

Other solution for testing emails are SMTP applications that can catch any message sent and display it in web interface. Most popular open-source solutions for SMTP testing are: MailCatcher ( and MailHog (

Emails in Symfony

Symfony 4 provides functionality to define email templates using TWIG templates. Common parts as header, footer are defined as blocks. Sometimes you want to display image in the email, you can embed them or link using url. Embedding images will make email bigger and can negatively affect deliverability. We are going to use SendGrid Image Editor, which is adding images to CDN. Image uploaded to CDN – we can use in symfony TWIG templates.

Automated email testing

It is recommended to test email rendering in the most popular Clients like: Apple iPhone, Gmail, Apple Mail, Outlook, Yahoo! Mail and Google Android. Previewing email design, SPAM score verification, checking sender reputation – are the functionalities provided by online services like Litmus, Mailtrap, or Email on Acid. By optimizing the emails you will increase email open rate and minimize possibility to mark it as SPAM.

Preview emails in the browser

Imagine – that you can use one url: /emails-preview – to immediately see all emails from the Symfony PHP system. Every change done in html.twig email templates will be visible on the preview link. We’re going to loop through all the templates, pass needed twig variables and render HTML source.


 * @Route("/emails-preview", name="my_app_emails_preview", options={"expose"=true})
 * @param Request $request
 * @return JsonResponse
public function emailPreview(Request $request): Response
    $env = $this->getParameter('kernel.environment');
    if($env != 'dev') {
        return new Response("Forbidden", 403);

    // register confirmation
    $user = $this->getUser();
    $tokenTest = 'FZlLTNfly1C0RnrB1lNSPIt8G1gj46eTfdBaTFNE';

    // results
    $userResults = array(
        array("event" => array("name" => "Event test 1234"), "users" =>"User1, User2, User3"),
        array("event" => array("name" => "Some Event lorem ipsum"), "users" =>"User 4"),

    // prices
    $pricePack = [
        'value' => 4.99,
        'currency' => ['code'=> 'USD']

    $templates = [
        // TESTING
        // FOS USER BUNDLE
        // EVENTS
        // USER

    // set language
    $request->getSession()->set('_locale', 'EN_en');

    $result = '';
    $counter = 0;

    foreach($templates as $key => $item):

        if($counter != 1) {
            // continue;

        $result .= '<h2 style="color:#fff; font-size:18px; padding:20px 3px 3px 3px; display:flex; justify-content: center; align-items: center; border-top:2px solid #333; ">
                        <span style="height:30px; width:30px; line-height: 30px; font-weight: bold; border-radius:50%; background:#333; color:#fff; margin-right:5px;font-size: 12px; text-align: center;">'.$counter . '</span>'. $item . '</h2>';

        $result .= $this->renderView(
                'link' => '',
                'description' => 'some description 123',
                'job' => 'jobName 56',
                'salaryExpectation' => '23',
                'phone' => '323 44 5 666',
                'email_address' => '[email protected]',
                'lastName' => 'Lorem ips 34',
                'firstName' => 'My name 123',
                'pricePack' => $pricePack,
                'friend' => $user,
                'userResults' => $userResults,
                'sender' => $user,
                'receiver' => $user,
                'message' => 'Some message here lorem ipsum',
                'subject' => "Some subject lorem ipsum",
                'confirmationUrl' => '',
                'token' => $tokenTest,
                'username' => $user,
                'user' => $user,

    return new Response(




About the article Author:

Maciej Brzeziński

Techleader of createIT, working in the IT sector for over 14 years. Innovator, author, and owner of an e-shop that he has been continuously developing for over 10 years. Currently, with his team of over 20 developers, he supervises all kinds of e-commerce projects of varied structure, characteristics, and size: from simple on-line shops for retail clients to advanced B2B platforms with support of multiple shopping carts, multiple languages, and integration with external systems. His work also involves modifying and expanding the existing platforms with millions of products in their databases, platforms that support variable pricing (several variants of the same product), dropshipping modules, integration with storage systems, integration with courier delivery services, and the last but not least, the optimization of scripts. By participating in a multitude of projects, he had the opportunity to “test out” all kinds of platforms (open-source, SaaS – Cloud-Hosted eCommerce Platforms) and dedicated platforms.

About us:

Hi, we are createIT!

As a passionate group of professionals with a love for Web and mobile technologies, we have been successfully serving our clients for the past 15 years. Through these years we have continuously striven to create the best IT solutions our clients seek.

Our 70+ developer team creates cutting-edge complex B2B and B2C web systems and apps as well as delivers top-notch direct outsourcing services.

We work in multiple frameworks but we hold special feelings and are experts in PHP (Symfony, OXID, WordPress), JavaScript (React, AngularJS), Flutter and Blockchain. We do enjoy working in the scalable cloud environment like AWS.

We are located in Warsaw, Poland, in the heart of Europe. Concentrated on delivering durable web solutions of high integrity, we do not always try to be the cheapest. However, thanks to our location you will be surprised how reasonable our prices are.

Fluent in English we offer services worldwide. Among others, in countries such as the U.S.A., Great Britain, Germany, Australia, Sweden and Poland.

Need help?

  • Looking for support from experienced programmers?

  • Need to fix a bug in the code?

  • Want to customize your webste/application?


Your email address will not be published.

createIT Contact