如何在核心PHP的邮件模板中包含CSS?

时间:2017-11-22 06:15:01

标签: php

我在html中创建了邮件模板

  

的test.html

<html>
<head>
    <meta charset="utf-8">
    <title>Easy Donation</title>
</head>
<body>
    <header>
        <h1>Easy Donation Receipt</h1>          
    </header>

    <h1>Thanks For Donating!</h1>
    <table class="donation-receipt">
        <tr>
            <th>First Name:</th>
            <td>{{test}}</td>
        </tr>
        <tr>
            <th>Last Name:</th>
            <td>{{test}}</td>
        </tr>                       
    </table>
</body>

内部CSS

<style>
body {
    border: 1px solid black;
    padding: 20px;
    display: inline-block;
}
</style>
  

mail.php

$to = "example@gmail.com";
$subject = "Example test";
$message = file_get_contents("test.html");
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

mail($to,$subject,$message,$headers);

工作正常我收到邮件但是样式表(内部CSS)没有应用于邮件模板。

提前致谢。

3 个答案:

答案 0 :(得分:0)

快速解决方案是将你的html代码直接放在这样的php变量中:

$my_var = <<<EOT
<html>
<head>
<meta charset="utf-8">
<title>Easy Donation</title>
</head>
<body>
<header>
    <h1>Easy Donation Receipt</h1>          
</header>

<h1>Thanks For Donating!</h1>
<table class="donation-receipt">
    <tr>
        <th>First Name:</th>
        <td>{{test}}</td>
    </tr>
    <tr>
        <th>Last Name:</th>
        <td>{{test}}</td>
    </tr>                       
</table>
</body>
EOT;

它始终有效。还要把你的css放在头部。

答案 1 :(得分:0)

尝试编写内联css。

示例:

<body style="border: 1px solid black;padding: 20px;display: inline-block;"></body>

答案 2 :(得分:0)

内部CSS可能无法在所有邮件门户中使用,因此根据需要添加内联css是更好的方法。它会工作。例如:

<body style=" border: 1px solid black;" >

--Body element--
</body>