PHP HTML电子邮件类选择器不工作GMAIL


PHP HTML email class selector not working GMAIL

我有通过PHP发送HTML电子邮件的代码。它有一张桌子。我想在屏幕截图中加粗四舍五入的部分。但是,到达Gmail的电子邮件并没有显示出应该应用于"about"类选择器的粗体和其他更改。

收到的电子邮件截图

我可以在PHP HTML电子邮件的样式表中使用类或ID选择器吗?我不喜欢使用内联CSS,因为它效率低下。我在身体之间用过它们。这是代码;

$to = "xxxx@gmail.com, xxxx@gmail.com"; //multiple recipients with comma separated
$subject = "Here is the subject"; //cannot contain newline characters
$message = "<h1>This is Heading H1</h1>";
$message .= "This is my <b>first</b> line of text.<br>This is my <b>second</b> line of text<br><br>"; //each line should be separated with ('n).  
$message .= "<html>
                <head>
                    <style>
                        body{
                            color:#000000;
                            background-color:#FFFFFF;
                            font-family:arial, verdana, sans-serif;
                        }
                        h1{
                            font-size:18px;
                        }
                        p{
                            font-size:12px;
                        }
                        table{
                            background-color:#efefef;
                            border-style:solid;
                            border-width:1px;
                            border-color:#999999;
                        }
                        th{
                            background-color:#cccccc;
                            font-weight:bold;
                            padding:5px;
                        }
                        td{
                            padding:5px;
                        }
                        td.about{
                            font-family:courier, courier-new, serif;
                            font-weight:bold;
                        }
                    </style>
                </head>
                <body>
                    <table>
                        <tr>
                            <th> About </th>
                            <th> Description </th>
                        </tr>
                        <tr>
                            <td class='"about'"> Name </td>
                            <td> Andreas </td>
                        </tr>
                        <tr>
                            <td class='"about'"> Address </td>
                            <td> Germany </td>
                        </tr>
                        <tr>
                            <td class='"about'"> Message </td>
                            <td> I like your 8 day tour package. What are the rates? </td>
                        </tr>
                    </table>
                </body>
            </html>
            "; 
//headers like From, Cc, Bcc, Reply-to, Date ???????????
//$header = "From:raveen1231@gmail.com 'r'n"; 
$header = "From: RXXXXX CXXXXX <rxxxx1111@gmail.com> 'r'n"; //additional headers should be separated with ('r'n)
//$header .= "Cc: rxxxxx111@gmail.com 'r'n";
//$header .= "Bcc: rxxxxx222@gmail.com 'r'n";
//always specify MIME version, content type, and character set when sending HTML email
$header .= "MIME-Version: 1.0 'r'n";
$header .= "Content-type:text/html;charset=UTF-8 'r'n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n"; //////////////////////////////Pls Delete later
$retval = mail( $to, $subject, $message, $header );
if( $retval == true ){
    echo "Message sent successfully";
}
else{
    echo "Message could not be sent!";
}

HTML电子邮件是出了名的难以纠正;每个电子邮件客户端将以不同的方式处理样式。引用李的《我对建筑的了解》;编码HTML电子邮件模板(重点是作者的):

电子邮件客户端使用不同的呈现引擎来呈现HTML电子邮件:

  • Apple Mail、Outlook for Mac、Android Mail和iOS Mail使用WebKit
  • Outlook 2000/02/03使用Internet Explorer
  • Outlook 2007/10/13使用Microsoft Word(是的,Word!)
  • 网络客户端使用各自浏览器的引擎,例如Safari使用WebKit,Chrome使用Blink

Gmail删除<link>标签和<style>标签中的任何CSS,以及任何其他未内联的CSS。不仅仅是Gmail网络,还有原生的Gmail移动应用程序。

为了直接回答你的问题,Gmail去掉了任何<style>标签,所以你需要将CSS内联到HTML元素中。

有一些web服务可以为您内联CSS,例如

  • MailChimp
  • ZURB电子邮件基金会

此外,您可以使用诸如CssToInlineStyle之类的库来内联它,以便在代码中完全处理它。