├── LICENSE ├── converter.rb ├── README.md └── Premailer.class.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2016 Oliver Nassar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /converter.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby 2 | require 'rubygems' 3 | begin 4 | gem 'premailer' 5 | require 'premailer' 6 | require 'getopt/long' 7 | 8 | opt = Getopt::Long.getopts( 9 | ['--markup', Getopt::REQUIRED], 10 | ['--css_to_attributes', Getopt::BOOLEAN], 11 | ['--include_link_tags', Getopt::BOOLEAN], 12 | ['--include_style_tags', Getopt::BOOLEAN], 13 | ['--input_encoding', Getopt::OPTIONAL], 14 | ['--preserve_reset', Getopt::BOOLEAN], 15 | ['--preserve_styles', Getopt::BOOLEAN], 16 | ['--remove_classes', Getopt::BOOLEAN], 17 | ['--remove_comments', Getopt::BOOLEAN], 18 | ['--remove_ids', Getopt::BOOLEAN], 19 | ['--remove_scripts', Getopt::BOOLEAN], 20 | ['--replace_html_entities', Getopt::BOOLEAN], 21 | ['--with_html_string', Getopt::BOOLEAN] 22 | ) 23 | premailer = Premailer.new( 24 | opt['markup'].dup, 25 | :css_to_attributes => opt['css_to_attributes'], 26 | :include_link_tags => opt['include_link_tags'], 27 | :include_style_tags => opt['include_style_tags'], 28 | :input_encoding => opt['input_encoding'], 29 | :preserve_reset => opt['preserve_reset'], 30 | :preserve_styles => opt['preserve_styles'], 31 | :remove_classes => opt['remove_classes'], 32 | :remove_comments => opt['remove_comments'], 33 | :remove_ids => opt['remove_ids'], 34 | :remove_scripts => opt['remove_scripts'], 35 | :replace_html_entities => opt['replace_html_entities'], 36 | :with_html_string => opt['with_html_string'] 37 | ) 38 | puts premailer.to_inline_css 39 | rescue Gem::LoadError 40 | raise 'Premailer not loaded' 41 | end 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PHP-Premailer 2 | ============= 3 | 4 | ### Install Heads Up! 5 | I just rebuilt an EC2, and in so doing, realized that the latest version of premailer doesn't work with my library (not sure why). To get around this, I uninstalled the premailer gem, and reinstalled, using this: 6 | 7 | sudo gem install premailer -v 1.7.3 8 | 9 | That did it! 10 | 11 | ### Quick Example 12 | 13 | ``` php 14 | getConvertedHtml(); 22 | 23 | ``` 24 | 25 | ### What is the library useful for? 26 | 27 | This library is useful for converting your email templates (which use `style` blocks for defining the UI) into sendable markup (with inline styles inserted in the right places, based on those `style` blocks). 28 | 29 | ### What does this library do? 30 | 31 | It goes through any `style` blocks, and searches your markup/html for those elements. It then inserts those styles inline, to make sure it's renderable by the highest number of email clients. 32 | 33 | ### The backstory 34 | 35 | While developing some emails for my recent project, [Podium](http://hellopodium.com/), I stumbled on [alexdunae](https://github.com/alexdunae)'s wonderful tool [Premailer](http://premailer.dialect.ca/). This tool allows you copy/paste markup with `style` tags, and have inline-styled code returned back. 36 | 37 | I looked around for some libraries to do that programatically in PHP, finding two. [CssToInlineStyles](https://github.com/tijsverkoyen/CssToInlineStyles) and [InlineStyle](https://github.com/christiaan/InlineStyle). Neither of these worked for my situation, so I decided to provide a PHP proxy/wrapper for [alexdunae](https://github.com/alexdunae/premailer)'s open source version (accessible here: ). 38 | 39 | Here it is. 40 | I hope you find it useful. 41 | 42 | ### Example Walkthrough 43 | 44 | ``` php 45 | getConvertedHtml(); 53 | 54 | ``` 55 | 56 | That's it. 57 | The library works by generating `bash` code to call the `ruby` script. That script then uses the open source `premailer` gem to make the conversions. Here is an example of before/after: 58 | 59 | **Before:** 60 | 61 | ``` html 62 | 75 |
Nassar
76 | ``` 77 | 78 | **After (raw):** 79 | 80 | ``` html 81 |
Nassar
82 | ``` 83 | 84 | **After (formatted):** 85 | 86 | ``` html 87 | 88 | 89 | 90 | 91 | 92 |
Nassar
93 | 94 | 95 | ``` 96 | 97 | See for the options the PHP library supports (almost all of them). 98 | 99 | If you have any feedback, please reach out: 100 | 101 | #### Heads up 102 | I noticed a strange bug. While using the open source ruby gem on a VM on my OSX machine, everything went smoothly with the `remove_classes` and `remove_ids` options. They worked as expected (eg. the styles would be applied, and the classes or ids would be removed after the styles had been applied). 103 | 104 | On a different server, however, there was some inconsitant behaviour. Specifically, if these options were set to `true`, some styles would or wouldn't get applied. 105 | 106 | I'm not sure if the ids/classes were getting removed before the conversion was being done, or what. There was a gem I thought was a prerequisite, called `hpricot`, that I installed on the latter server, that the former didn't have. 107 | 108 | I'm not sure if that's related, but a heads up if you notice that behaviour. 109 | -------------------------------------------------------------------------------- /Premailer.class.php: -------------------------------------------------------------------------------- 1 | 7 | * @see https://github.com/alexdunae/premailer/ 8 | * @see http://premailer.dialect.ca/ 9 | */ 10 | class Premailer 11 | { 12 | /** 13 | * _markup 14 | * 15 | * @access protected 16 | * @var null|string (default: null) 17 | */ 18 | protected $_markup = null; 19 | 20 | /** 21 | * _options 22 | * 23 | * @note Some servers bugged outwhen the following were turned on: 24 | * - remove_classes 25 | * - remove_ids 26 | * @see https://github.com/premailer/premailer/blob/master/lib/premailer/premailer.rb 27 | * @access protected 28 | * @var array 29 | */ 30 | protected $_options = array( 31 | 'css_to_attributes' => true, 32 | 'include_link_tags' => true, 33 | 'include_style_tags' => true, 34 | 'input_encoding' => 'ASCII-8BIT', 35 | 'preserve_reset' => true, 36 | 'preserve_styles' => true, 37 | 'remove_classes' => false, 38 | 'remove_comments' => false, 39 | 'remove_ids' => false, 40 | 'remove_scripts' => true, 41 | 'replace_html_entities' => false 42 | ); 43 | 44 | /** 45 | * __construct 46 | * 47 | * @access public 48 | * @return void 49 | */ 50 | public function __construct() 51 | { 52 | } 53 | 54 | /** 55 | * _getCLIOptions 56 | * 57 | * @access protected 58 | * @return array 59 | */ 60 | protected function _getCLIOptions(): array 61 | { 62 | $cliOptions = array(); 63 | $escapedMarkup = $this->_getEscapedMarkup(); 64 | $this->_options['markup'] = '"' . ($escapedMarkup) . '"'; 65 | $this->_options['with_html_string'] = true; 66 | $options = $this->_options; 67 | foreach ($options as $key => $value) { 68 | if ($value === false) { 69 | continue; 70 | } 71 | if ($value === true) { 72 | $cliOption = '--' . ($key); 73 | array_push($cliOptions, $cliOption); 74 | continue; 75 | } 76 | $cliOption = '--' . ($key) . ' ' . ($value); 77 | array_push($cliOptions, $cliOption); 78 | } 79 | return $cliOptions; 80 | } 81 | 82 | /** 83 | * _getCLIOptionsString 84 | * 85 | * @see http://rubyforge.org/docman/view.php/735/281/README.html 86 | * @access protected 87 | * @return string 88 | */ 89 | protected function _getCLIOptionsString() 90 | { 91 | $options = $this->_getCLIOptions(); 92 | $options = implode(' ', $options); 93 | return $options; 94 | } 95 | 96 | /** 97 | * _getEscapedMarkup 98 | * 99 | * @access protected 100 | * @return string 101 | */ 102 | protected function _getEscapedMarkup(): string 103 | { 104 | $markup = $this->_markup; 105 | $escapedMarkup = str_replace('"', '\"', $markup); 106 | return $escapedMarkup; 107 | } 108 | 109 | /** 110 | * getInlinedMarkup 111 | * 112 | * @access public 113 | * @return string 114 | */ 115 | public function getInlinedMarkup() 116 | { 117 | $scriptPath = dirname(__FILE__) . '/converter.rb'; 118 | $output = array(); 119 | $returnVar = 0; 120 | $command = ($scriptPath) . ' ' . $this->_getCLIOptionsString(); 121 | $response = exec($command, $output, $returnVar); 122 | if ($returnVar === 1) { 123 | $msg = 'Error'; 124 | throw new Exception($msg); 125 | } 126 | $response = implode("\n", $output); 127 | return $response; 128 | } 129 | 130 | /** 131 | * setMarkup 132 | * 133 | * @access public 134 | * @param string $markup 135 | * @return void 136 | */ 137 | public function setMarkup(string $markup): void 138 | { 139 | $this->_markup = $markup; 140 | } 141 | 142 | /** 143 | * setOption 144 | * 145 | * @see https://github.com/alexdunae/premailer/blob/master/lib/premailer/premailer.rb#L164 146 | * @access public 147 | * @param string $key 148 | * @param mixed $value 149 | * @return void 150 | */ 151 | public function setOption(string $key, $value): void 152 | { 153 | $this->_options[$key] = $value; 154 | } 155 | } 156 | --------------------------------------------------------------------------------