JANorman
Copy of pelagios emogrifier. Converts CSS styles into inline style attributes in your HTML code
Clone of the original library....
n. e•mog•ri•fi•er [\ē-'mä-grƏ-,fī-Ər] - a utility for changing completely the nature or appearance of HTML email, esp. in a particularly fantastic or bizarre manner
Emogrifier converts CSS styles into inline style attributes in your HTML code. This ensures proper display on email and mobile device readers that lack stylesheet support.
This utility was developed as part of Intervals to deal with the problems posed by certain email clients (namely Outlook 2007 and Google Gmail) when it comes to the way they handle styling contained in HTML emails. As many web developers and designers already know, certain email clients are notorious for their lack of CSS support. While attempts are being made to develop common email standards, implementation is still a ways off.
The primary problem with uncooperative email clients is that most tend to only regard inline CSS, discarding all
<style>
elements and links to stylesheets in <link>
elements. Emogrifier solves this problem by converting CSS
styles into inline style attributes in your HTML code.
Emogrifier automagically transmogrifies your HTML by parsing your CSS and inserting your CSS definitions into tags within your HTML based on your CSS selectors.
First, you provide Emogrifier with the HTML and CSS you would like to merge. This can happen directly during instantiation:
$html = '<html><h1>Hello world!</h1></html>';
$css = 'h1 {font-size: 32px;}';
$emogrifier = new \Pelago\Emogrifier($html, $css);
You could also use the setters for providing this data after instantiation:
$emogrifier = new \Pelago\Emogrifier();
$html = '<html><h1>Hello world!</h1></html>';
$css = 'h1 {font-size: 32px;}';
$emogrifier->setHtml($html);
$emogrifier->setCss($css);
After you have set the HTML and CSS, you can call the emogrify
method to merge both:
$mergedHtml = $emogrifier->emogrify();
Download the composer.phar
locally or install Composer globally:
curl -s https://getcomposer.org/installer | php
Run the following command for a local installation:
php composer.phar require pelago/emogrifier:*
Or for a global installation, run the following command:
composer require pelago/emogrifier:*
You can also add follow lines to your composer.json
and run the composer update
command:
"require": {
"pelago/emogrifier": "*"
}
See https://getcomposer.org/ for more information and documentation.
Emogrifier currently support the following CSS selectors:
The following selectors are implemented, but currently are broken:
The following selectors are not implemented yet:
<style>
blocks from your HTML, but it
will not grab CSS files referenced in elements (the problem email clients are going to ignore these tags
anyway, so why leave them in your HTML?).Emogrifier is maintained by the good people at Pelago, info AT pelagodesign DOT com.
Those that wish to contribute bug fixes, new features, refactorings and clean-up to Emogrifier are more than welcome. When you contribute, please take the following things into account: