Theres is a way...
$body = <<< YOUR_HTML_WITH_CSS_STYLE_TAGS
<html>
<head>
<style>
body * {width:1px;}
#adiv {padding:2px;}
.aclass {margin:3px;}
</style>
</head>
<body>
<div>
some html
</div>
<div id="adiv">
<p class="aclass">
</p>
</div>
</body>
</html>
YOUR_HTML_WITH_CSS_STYLE_TAGS;
$doc = new DOMDocument();
@$doc->loadHTML($body);
$xpd = new DOMXPath($doc);
0&&$node = new DOMElement();
$result = $xpd->query('//img');
foreach($result as $node){
$attr = $node->getAttribute('src');
$re = '/(http:\/\/.*?)?(\/.*+)/i';
if(preg_match_all($re, $attr, $matches)){
if(!empty($matches[1][0])&&0)
continue;
$attr = 'http://'.$_SERVER['HTTP_HOST'].$matches[2][0];
}
$node->setAttribute('src',$attr);
}
false&&$node=new DOMElement()&&$child=new DOMElement();
$result = $xpd->query('//style/..');
foreach($result as $node){
foreach($node->childNodes as $child){
if(strtolower($child->nodeName)=='style'){
$node->removeChild($child);
$css = $child->textContent;
$re = '/(.*?)\{([^}]+)\}/';
if(preg_match_all($re, $css, $matches)){
foreach($matches[1] as $idx=>$css_selector){
$css_text = $matches[2][$idx];
$css_text = preg_replace('/\s+/',' ',$css_text);
$css = new CSSQuery($doc);
foreach($css->query($css_selector) as $selected_node){
$style = $selected_node->getAttribute('style');
$selected_node->setAttribute('style', $style?$css_text:$style.';'.$css_text);
}
}
}
}
}
}
$body = $doc->saveHTML();
That code will generate an HTML output in $body like this:
<html>
<head>
</head>
<body>
<div style="width:1px;">
some html
</div>
<div id="adiv" style="width:1px;padding:2px;">
<p class="aclass" style="width:1px;margin:3px;">
</p>
</div>
</body>
</html>
the CSSQuery class can be found at phpclasses.org .
This implementation is based on the fact that most webmails will only allow to add style by inline tag attributte style and not through style tags or link tags.
Its pretty much limited an with a restricted sintax because of the regexp its kind of simple but its still better than write by your own the inline style attributes in each html tag.