fullpipe/email-template-bundle

将电子邮件模板保存在仓库中并按顺序排列

安装: 7

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2015-02-16 08:48 UTC

This package is auto-updated.

Last update: 2024-08-27 00:39:24 UTC


README

将电子邮件模板保存在仓库中并按顺序排列。

安装

首先,您需要将 fullpipe/email-template-bundle 添加到 composer.json

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

您还必须将 EmailTemplateBundle 添加到您的 AppKernel.php

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

        return $bundles;
    }
    //...
}

配置

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
        #...

用法

例如,我们需要在用户注册后发送确认电子邮件。首先,我们需要添加模板配置

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

其次,我们需要创建 AcmeWebBundle:EmailTemplates:reg_confirmation.html.twig

{% 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 %}

然后我们可以发送我们的电子邮件

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

好处

  • 如果在twig模板块 {% block body_text %}...{% endblock %} 中缺少,并且选项 generate_text_versiontrue,则文本版本将由 html2text 生成
  • html版本由CssToInlineStyles处理