crossjoin/pre-mailer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

crossjoin/pre-mailer

最新稳定版本:v1.0.5

Composer 安装命令:

composer require crossjoin/pre-mailer

包简介

Crossjoin\PreMailer converts CSS in a given HTML source to inline styles and optimizes it for sending it via e-mail. It also creates a text version of the HTML source. Requires PHP 5.4+.

README 文档

README

Introduction

Crossjoin\PreMailer converts CSS in a given HTML source to inline styles and optimizes it for sending it via e-mail. It also creates a text version of the HTML source.

Installation

This is a composer package. See the composer website for basic installation information.

Add the following line to your composer.json file:

{
    "require": {
        "crossjoin/pre-mailer": "1.0.*"
    }
}

Features

  • Extracts CSS from HTML sources
  • Can move CSS to the body of the HTML document (so it won't be removed by some e-mail clients)
  • Can remove comments from the HTML document
  • Can remove all class attributes from the HTML document
  • Compresses the CSS
  • Creates a text version of the HTML document (for the alternative part of the e-mail)
  • ...

Usage

Reading HTML

You can read different HTML sources.

// Read HTML file
$htmlFileName = "path/to/file.html";
$preMailer = new \Crossjoin\PreMailer\HtmlFile($htmlFileName);

// Read HTML string
$htmlString = "<html>...</html>";
$preMailer = new \Crossjoin\PreMailer\HtmlString($htmlString);

Set charset

The default charset is "UTF-8". You can change it to the preferred charset.

// Sets the charset of the HTML file.
$preMailer->setCharset("ISO-8859-1");

Set options

You can set different options to influence the PreMailer behavior.

// Remove HTML comments (default)
$preMailer->setOption($preMailer::OPTION_HTML_COMMENTS, $preMailer::OPTION_HTML_COMMENTS_REMOVE);

// Keep HTML comments
$preMailer->setOption($preMailer::OPTION_HTML_COMMENTS, $preMailer::OPTION_HTML_COMMENTS_KEEP);

// Move the style tag to the body of the HTML document (default)
$preMailer->setOption($preMailer::OPTION_STYLE_TAG, $preMailer::OPTION_STYLE_TAG_BODY);

// Move the style tag to the head of the HTML document
$preMailer->setOption($preMailer::OPTION_STYLE_TAG, $preMailer::OPTION_STYLE_TAG_HEAD);

// Remove the style tag from the HTML document
// (to use, if ALL of your styles can be written inline)
$preMailer->setOption($preMailer::OPTION_STYLE_TAG, $preMailer::OPTION_STYLE_TAG_REMOVE);

// Keep HTML class attributes (default)
$preMailer->setOption($preMailer::OPTION_HTML_CLASSES, $preMailer::OPTION_HTML_CLASSES_KEEP);

// Remove HTML class attributes
$preMailer->setOption($preMailer::OPTION_HTML_CLASSES, $preMailer::OPTION_HTML_CLASSES_REMOVE);

// Set line-width of the text version (defaults to 75)
$preMailer->setOption($preMailer::OPTION_TEXT_LINE_WIDTH, 60);

// Set CSS writer class (class that extends \Crossjoin\Css\Writer\WriterAbstract).
// By default the Compact writer (\Crossjoin\Css\Writer\Compact) is used, but for
// some purposes another writer (like \Crossjoin\Css\Writer\Pretty) can be useful.
$preMailer->setOption($preMailer::OPTION_CSS_WRITER_CLASS, $preMailer::OPTION_CSS_WRITER_CLASS_PRETTY);
// Instead of the constant also the full class name can be used:
$preMailer->setOption($preMailer::OPTION_CSS_WRITER_CLASS, '\Crossjoin\Css\Writer\Pretty');
// So you can use your own writer if required:
$preMailer->setOption($preMailer::OPTION_CSS_WRITER_CLASS, '\MyNameSpace\Css\Writer\MyWriter');

Generate the content

The PreMailer generated an optimized HTML and text version for the e-mail.

// Get the HTML version
$html = $preMailer->getHtml();

// Get the text version
$text = $preMailer->getText();

To Do

  • Add charset auto-detection (extracted from the HTML document)
  • Ability to influence the text version format
  • Optimize inline styles (remove declarations that are overwritten within the same inline style)

crossjoin/pre-mailer 适用场景与选型建议

crossjoin/pre-mailer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.98k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2015 年 05 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mail」 「php」 「text」 「css」 「html」 「styles」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 crossjoin/pre-mailer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 crossjoin/pre-mailer 我们能提供哪些服务?
定制开发 / 二次开发

基于 crossjoin/pre-mailer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 23
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 1
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-27