gpt4 book ai didi

php - 样式化来自 php 表单的电子邮件

转载 作者:行者123 更新时间:2023-12-04 22:25:09 26 4
gpt4 key购买 nike

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

incoming email screen shot

有没有办法可以使用 CSS 设置样式,即。将发件人和电子邮件标题设为粗体 {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>";
?>

最佳答案

您可以像这样轻松地在 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);
?>

关于php - 样式化来自 php 表单的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11755521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com