trt/swift-css-inliner-bundle

一个提供CSS内联功能的Swiftmailer插件

v0.4 2015-09-15 07:49 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:53:11 UTC


README

Latest Stable Version Build Status Scrutinizer Quality Score Total Downloads

当在基于浏览器的电子邮件应用程序(如YahooMail!、Gmail、Hotmail等)中查看HTML电子邮件时,这些应用程序默认会删除HEAD和BODY标签,因此唯一的样式内容的方式是将CSS内联到style属性中。这对于前端来说是一项繁琐的工作。此插件正好提供了所需的CSS处理过程以填充style属性。

例如,使用Zurb ink邮件模板Before After Plugin

#1. 安装

####在composer.json中添加依赖项

    "require": {
        "trt/swift-css-inliner-bundle": "~0.3"
    }

运行php composer.phar install

####启用插件(在AppKernel.php中添加以下行)

$bundles = array(
    [...]
    new \Trt\SwiftCssInlinerBundle\TrtSwiftCssInlinerBundle(),
);

#2. 使用 - 完整示例

/**
 * @Route("/hello/{name}", name="_demo_hello")
 */
public function helloAction($name)
{
    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('send@example.com')
        ->setTo('recipient@example.com')
        ->setContentType('text/html')
        ->setBody("<style>.text{ color: red; }</style><p class='text'> $name </p>")
    ;
    $message->getHeaders()->addTextHeader(
        CssInlinerPlugin::CSS_HEADER_KEY_AUTODETECT
    );

    $this->get('mailer')->send($message);
}

#3. 使用 - 分步示例

####1. 创建swiftmailer消息。

$message = \Swift_Message::newInstance()
    ->setSubject('Hello Email')
    ->setFrom('send@example.com')
    ->setTo('recipient@example.com')
    ->setContentType('text/html')
    ->setBody('<style>.text{color:red;}</style>  <p class="text"> Hello </p>')
;

####2. 自动检测"style"Html标签

自动检测模式将在style标签中找到CSS

$message->getHeaders()->addTextHeader(
    CssInlinerPlugin::CSS_HEADER_KEY_AUTODETECT
);

明确添加您的样式

注意

明确模式仅适用于已安装php IMAP扩展的情况 @see https://php.ac.cn/manual/en/book.imap.php

$message->getHeaders()->addTextHeader(
    CssInlinerPlugin::CSS_HEADER_KEY, //The key that say to the plugin "Apply this CSS"
    ".text{ color: red; }"
);

发送消息。

$this->get('mailer')->send($message);

#4. 配置选项

不需要配置。

可以自定义CssToInlineStyles类的行为。以下显示默认选项

# app/config/config.yml
trt_swift_css_inliner:
    inliner_class: TijsVerkoyen\CssToInlineStyles\CssToInlineStyles
    cleanup: false
    strip_original_style_tags: false
    exclude_media_queries: true