php - 如何使用 PHPMailer 发送包含 CSS 的 HTML 邮件?

标签 php html css phpmailer

我在使用 PHPMailer 发送 HTML 邮件时遇到问题.我做了一个 Smarty模板,我从中得到了所有的 HTML 代码。但是当我发送邮件时,我收到的邮件没有包含 CSS(它只是背景颜色、字体或类似的东西)。在 PHPMailer 中,我将邮件设置为 HTML。

有什么方法可以发送包含 CSS 的 HTML 邮件吗?

最佳答案

有办法...

    $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();

该代码将在 $body 中生成一个 HTML 输出,如下所示:

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

CSSQuery 类可以在 phpclasses.org 找到. 此实现基于这样一个事实,即大多数网络邮件只允许通过内联标签属性样式添加样式,而不是通过样式标签或链接标签添加样式。

由于正则表达式的原因,它非常有限并且语法受限,它有点简单,但它仍然比您自己编写每个 HTML 标记中的内联样式属性要好。

关于php - 如何使用 PHPMailer 发送包含 CSS 的 HTML 邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/358788/

相关文章:

php - 如何使用我的代码区分大小写登录?

javascript - 如何判断在表中的 href 中单击了哪个行号?

html - Css 为什么我的 <a> 标签不在我的 "div"中间。 valign=middle 不起作用

html - 使用 Bootstrap 布局网格

php - 在 Joomla 中,如何为 index.php 创建 3x3(9 格)代码?

php - 使用post和response从iPhone获取NSMutableArray到PHP

php - 将数组作为参数列表传递给非用户定义的函数

css - 如何使元素从左侧淡入和反弹?

html - 带有 1 个大列的 Twitter Bootstrap 网格

javascript - 将 flash div 调整为完整的浏览器高度和宽度?