2017 © Pedro Peláez
 

symfony-bundle email-template-bundle

Keep email templates in repo and in order

image

fullpipe/email-template-bundle

Keep email templates in repo and in order

  • Monday, February 16, 2015
  • by fullpipe
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

About EmailTemplateBundle

Keep email templates in repo and in order., (*1)

Installation

First you need to add fullpipe/email-template-bundle to composer.json:, (*2)

{
   "require": {
        "fullpipe/email-template-bundle": "dev-master"
    }
}

You also have to add EmailTemplateBundle to your AppKernel.php:, (*3)

// app/AppKernel.php
class AppKernel extends Kernel
{
    //...
    public function registerBundles()
    {
        $bundles = array(
            //...
            new Fullpipe\ImageBundle\FullpipeImageBundle()
        );

        return $bundles;
    }
    //...
}

Configuration

fullpipe_email_template:
    # default from email
    from: # or simply from: noreply@example.com
        email: noreply@example.com
        name: noreply

    # default no reply email
    reply_to: # or simply reply_to: reply@example.com
        email: reply@example.com
        name: We are waiting for your reply

    # default utm-marks for links in emails (optional)
    utm:
        utm_source: source_test
        utm_medium: medium_test
        utm_campaign: campaign_test

    # default host for which utm-marks will be applied
    host: %router.request_context.scheme%://%router.request_context.host%

    # Your templates
    # requires at least one template
    templates:
        default: # template name
            # template uri
            template: "FullpipeEmailTemplateBundle:Template:default.html.twig" 

            # custom utm-marks (optional)
            utm: 
                utm_source: source_default
                utm_medium: medium_default
                utm_campaign: campaign_default

            # custorm host (optional)
            host: http://example.com

            # generate text version automatically, true by default
            generate_text_version: true
        #...

Usage

For example we need to send confirmation email, after user registration. First, we need to add template config:, (*4)

fullpipe_email_template:
    #...
    templates:
        reg_confirmation:
            template: "AcmeWebBundle:EmailTemplates:reg_confirmation.html.twig"
            utm: 
                utm_source: reg_confirmation
                utm_medium: email

Second, we need to create AcmeWebBundle:EmailTemplates:reg_confirmation.html.twig:, (*5)

{% block subject %}Good to you, {{ username }}{% endblock %}
{% block body_html %}
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
            <style type="text/css">
                ...
            </style>
        </head>
        <body>
            <h1>Hello, {{ username }}</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magnam deserunt magni libero quas aperiam, labore harum, eos expedita, dolores autem illum? Fugiat, molestias, minus. Libero impedit fugit inventore, aliquid perspiciatis.</p>
        </body>
    </html>
{% endblock %}

And we could send our email:, (*6)

    $this->get('fullpipe_email_template.mailer')
        ->prepareMessage('reg_confirmation', array('username' => 'USERNAME!'))
        ->setTo('username@example.com', 'USERNAME!')
        ->send();

Profits

  • if in twig template block {% block body_text %}...{% endblock %} is missing and option generate_text_version is true then text version will be generated by html2text
  • html version is processed by CssToInlineStyles

The Versions