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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/358788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:29:30  来源:igfitidea点击:

How can I send HTML mails with included CSS with PHPMailer?

phphtmlcssphpmailer

提问by gizmo

I've problem with sending HTML mails with PHPMailer. I make a Smartytemplate 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.

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

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

有没有办法使用包含的 CSS 发送 HTML 邮件?

回答by useless

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:

该代码将在 $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>

The CSSQueryclass 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.

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

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.

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

回答by Aron Rotteveel

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

至少,电子邮件中的 CSS 支持非常有限。最大的问题是不同的客户端支持不同的 CSS 属性集。

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?
  • 您的电子邮件如何显示?CSS根本没有被解析吗?你的 CSS 在屏幕上显示为文本吗?
  • 你的 CSS 看起来怎么样?
  • 您的电子邮件模板看起来如何?

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

有关电子邮件中 ​​CSS 支持的更多信息,请参阅此优秀概述。

回答by roborourke

Some Email Clients will strip out the <head> section so put your <style></style> tags within the <body>.

一些电子邮件客户端会去掉 <head> 部分,因此将您的 <style></style> 标签放在 <body> 中。

回答by Raithlin

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

我发现对 CSS 的最佳(阅读最广泛)支持是内联(style="")。悲伤,但真实。

回答by Ronald Conco

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

这是一篇关于 HTML 电子邮件的非常好的SitePoint文章,“如何编写HTML 电子邮件通讯”。

回答by annakata

HTML and CSS is fraught with pain and frustration. Nothing to do with PHP, it's apparent that most implementations coughoutlookcoughwere and remain archaic.

HTML 和 CSS 充满了痛苦和挫折。无关,与PHP,这是显而易见的是,大多数实现咳嗽前景咳嗽过去和现在过时了。

This is the only area where I would advise this (and someone else might have a better understanding/plan*), but you should look at cutting the CSS and writing mid-90's style html with <table>, <font> and <hr> tags (oh my)

这是我建议的唯一领域(其他人可能有更好的理解/计划*),但是您应该考虑使用 <table>、<font> 和 <hr 削减 CSS 并编写 90 年代中期风格的 html > 标签(天哪)

** please share :)*

** 请分享 :)*

回答by Neil Aitken

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

对于电子邮件,您要么必须提供样式表的绝对路径,要么在模板的头部包含样式

回答by grapefrukt

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.

我假设您的 CSS 位于外部文件中,如果是这样,最简单的解决方案是将其移动到邮件内的 html 标题中。

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

但是,电子邮件客户端中的 css 支持非常不稳定,因此它们可能只是糟糕的渲染。