How can I send HTML mails with included CSS with P

2020-02-11 06:09发布

I've 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.

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

8条回答
来,给爷笑一个
2楼-- · 2020-02-11 06:25

How is your stylesheet referenced?

For email you will either have to provide an absolute path to your stylesheet or include the styles in the head of the template

查看更多
我想做一个坏孩纸
3楼-- · 2020-02-11 06:28

Here is a really good SitePoint article on HTML emails, "How to Code HTML Email Newsletters".

查看更多
相关推荐>>
4楼-- · 2020-02-11 06:31

CSS support in e-mail is very limited, at least. The biggest issue is that different clients support different sets of CSS-properties.

You provide very little context for us to work with.

  • How is your e-mail showing? Is CSS not parsed at all? Is your CSS showing on-screen as text?
  • How does your CSS look?
  • How does your e-mail template look?

For more information on CSS support in e-mail, please refer to this excellent overview.

查看更多
戒情不戒烟
5楼-- · 2020-02-11 06:33

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

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.

查看更多
太酷不给撩
6楼-- · 2020-02-11 06:34

I assume you have your CSS in an external file, if so the easiest solution would be to simply move it into the html header inside the mail.

However, css support in email clients is very wonky, so it might just be crappy rendering on their part.

查看更多
▲ chillily
7楼-- · 2020-02-11 06:40

I've found the best (read broadest) support for CSS is inline (style=""). Sad, but true.

查看更多
登录 后发表回答