sam*_*sam 2 css php forms contact-form
我在网站上有一个表单,允许用户输入他们的姓名,电话,电子邮件等.当我收到这些信息作为电子邮件时,它完全没有格式化,所以它有点难以阅读.见下图:

有没有办法可以使用CSS来设置这个样式,即.使From和电子邮件标题大胆{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>";
?>
Run Code Online (Sandbox Code Playgroud)
您可以像这样在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);
?>
Run Code Online (Sandbox Code Playgroud)
您必须使用嵌入在 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>"
Run Code Online (Sandbox Code Playgroud)
像这样将 CSS 放在这个头部:
<head>
<style type="text/css">
body { background-color: #ff0000; }
</style>
</head>
Run Code Online (Sandbox Code Playgroud)
显然,您必须使用这些: ' 而不是这些 " 并使用句号或逗号连接任何变量,以防您需要对任何 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>'
Run Code Online (Sandbox Code Playgroud)
编辑:
您还应该像这样更改标题:
$mailheader = "MIME-Version: 1.0" . "\r\n";
$mailheader .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$mailheader .= "From: $email \r\n";
Run Code Online (Sandbox Code Playgroud)