Coder Social home page Coder Social logo

marcinkrawiec / premailer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tekzenit/premailer

0.0 1.0 0.0 22 KB

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

License: MIT License

PHP 100.00%

premailer's Introduction

PreMailer

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)

premailer's People

Contributors

cziegenberg avatar marcinkrawiec avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.