3

我在一个网站上有一个表格,允许用户输入他们的姓名、电话、电子邮件等。当我收到此信息作为电子邮件时,它完全没有格式化,因此阅读起来有点困难。见下图:

收到的电子邮件屏幕截图

有没有办法可以使用 CSS 来设置它的样式,即。使 From 和 email 标题加粗{font-weight:bold;}

我用于表单的 php 是:

<?php
$name        = $_POST['name'];
$email       = $_POST['email'];
$phone       = $_POST['phone'];
$message     = $_POST['message'];
$formcontent ="From: $name \n Email: $email \n Phone: $phone \n Message: $message";
$recipient   = "studio@ll-i.co.uk";
$subject     = "Contact Form";
$mailheader  = "From: $email \r\n";

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

echo "<p>Thanks for getting in touch, we'll get back to you shortly..</p>";
?>
4

2 回答 2

6

您可以像这样在 HTML 中轻松地对其进行格式化 - 请注意,您可以编写将在内容中使用的内部 CSS:

<?php
$to = "somebody@example.com, somebodyelse@example.com";
$subject = "HTML email";

$message = "
<html>
    <head>
    <title>HTML email</title>
        <style type="text/css">
        hr {color:sienna;}
        p {margin-left:20px;}
        h3
        {
        color:red;
        text-align:left;
        font-size:8pt;
        }
        </style>
    </head>
<body>
    <h3>The fancy CSS heading!</h3>
    <p>".$email."</p>
    <hr>
    <table>
        <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        </tr>
";
// You can even do stuff like this:
for($i=0;$i<count($someArrayFromYourForm);$i++)
{
    $email.="
        <tr>
        <td>".$someArrayFromYourForm['formField']."</td>
        <td>".$someArrayFromYourForm['formField2']."</td>
        </tr>
";
}
$email.="
    </table>
</body>
</html>
";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

// More headers
$headers .= 'From: <webmaster@example.com>' . "\r\n";
$headers .= 'Cc: myboss@example.com' . "\r\n";

mail($to,$subject,$message,$headers);
?> 
于 2012-08-01T08:39:44.780 回答
5

您必须使用嵌入在 HTML 文件中的内联 CSS / CSS 对其进行样式设置。

$formcontent 变量可以包含 HTML,例如

"<html>
    <head>
        <title>Message</title>
    </head>
    <body>
        <p><strong>From:</strong> $name</p>
        <p><strong>Email:</strong> $email</p>
        <p><strong>Phone:</strong> $phone</p>
        <p><strong>Message:</strong> $message</p>
    </body>
</html>"

像这样将 CSS 放在这个头部:

<head>
   <style type="text/css">
       body { background-color: #ff0000; }
   </style>
</head>

显然,您必须使用这些:' 而不是这些 ",并使用句号或逗号连接任何变量,以防您需要对任何 HTML 使用语音引号。

像这样:

'<html>
    <head>
        <title>Message</title>
        <style type="text/css">
           body { background-color: #ff0000; }
        </style>
    </head>
    <body>
        <p><strong>From:</strong> ',$name,'</p>
        <p><strong>Email:</strong> ',$email,'</p>
        <p><strong>Phone:</strong> ',$phone,'</p>
        <p><strong>Message:</strong> ',$message,'</p>
    </body>
</html>'

编辑:

您还应该像这样更改标题:

$mailheader = "MIME-Version: 1.0" . "\r\n";
$mailheader .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$mailheader .= "From: $email \r\n";
于 2012-08-01T08:40:17.763 回答