如何使用PHPMailer发送包含CSS的HTML邮件? [英] How can I send HTML mails with included CSS with PHPMailer?

查看:177
本文介绍了如何使用PHPMailer发送包含CSS的HTML邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have problem with sending HTML mails with PHPMailer. I make a Smarty template and I got all the HTML code from it, but when I send mail, I got the mail without included CSS (it's only background-color, font or something like that). In PHPMailer I set that the mail is HTML.

是否有任何方法发送包含CSS的HTML邮件?

Is there any way to send HTML mail with included CSS?

推荐答案

有一种方法...

    $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输出,如下所示:

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>

CSSQuery a href =http://www.phpclasses.org/ =nofollow noreferrer> phpclasses.org 。
此实现基于这样的事实,即大多数webmails只允许通过内联标记属性样式而不是通过样式标记或链接标记添加样式。

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 an inline tag attribute style and not through style tags or link tags.

它非常有限,并且由于正则表达式的限制语法,它很简单,但它仍然比写自己的每个HTML标签的内联样式属性更好。

It's pretty much limited and with a restricted syntax because of the regular expression it's kind of simple, but it's still better than write by your own the inline style attributes in each HTML tag.

这篇关于如何使用PHPMailer发送包含CSS的HTML邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆