illarra/email-bundle

此包已被废弃且不再维护。没有建议替代包。

在Symfony2中创建漂亮的电子邮件

安装次数: 3,824

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放问题: 3

类型:symfony-bundle

0.3.0 2013-07-07 10:15 UTC

This package is not auto-updated.

Last update: 2024-05-06 12:58:48 UTC


README

Build Status Total Downloads Latest Stable Version Latest Unstable Version

此扩展包允许您使用Twig作为模板语言创建具有内联样式的HTML电子邮件。它由两个服务组成:

  • 渲染器:使用Twig布局/模板和CSS更新给定的Swift_Message。
  • 邮件发送器:它是默认"@mailer"服务的包装器,允许您使用配置文件来指定谁发送电子邮件。
$message = \Swift_Message::newInstance();

$this->get('illarra.email.renderer')->updateMessage(
    $message,
    'AcmeEmailBundle:Email:layout.html.twig',
    'AcmeEmailBundle:Email:signup/eu.html.twig',
    '@AcmeEmailBundle/Resources/assets/css/email.css',
    [
        'name' => 'Bartolo',
    ]
);

$message->setTo(['bartolo@example.com' => 'Bartolo']);

$this->get('illarra.email.mailer')->send('maritxu', $message);

配置

config.yml:

illarra_email:
  # Force double quotes in HTML tag attributes, <p style=''> => <p style="">
  # This is usefull for Mandrill or other service which needs double quote attributes
  force_double_quotes: false

  # Generate plain text message from HTML version
  generate_plain: false

  # See "Renderer"
  layout_var:  'layout'
  subject_var: 'subject'

  # See "Mailer"
  profiles: ~

渲染器

$renderer->updateMessage($swift_message, $layout, $template, $css, $data);

布局 & 模板

布局和模板都需要一个Twig路径。布局和模板是分开的,这样更容易维护多个模板。

以下是$template所需的最小内容

{% extends layout %}
{% block subject %}Welcome {{ name }}!{% endblock %}

注意,在extends标签中的布局是updateMessage()方法中提供的$layout变量的对应变量。使用subject块生成电子邮件主题。两者都是必需的

布局变量和主题块的名称可以在config.yml中更改

illarra_email:
  layout_var:  'layout'
  subject_var: 'subject'

CSS

您可以使用@AcmeBundle/path/my.css表示法来定位您的CSS。此CSS文件将被用于向生成的HTML添加内联样式。

邮件发送器

$mailer->send($profile, $swift_message);

配置文件中定义了配置文件

illarra_email:
  profiles:
    maritxu:
      from: { maritxu@example.com: Maritxu }
    bartolo:
      from: { no-reply@example.com: Unknown }
      reply_to: { bartolo@example.com: Bartolo }

定义类似Swift Message中的From和ReplyTo选项:{'email': 'name'}。您可以在From参数中定义多个电子邮件,所有电子邮件都将对收件人可见,但只有第一个将是实际的发送者。