从电子邮件中删除HTML标记,并设计电子邮件正文

时间:2013-09-01 10:48:45

标签: php html email html-email

我有以下php代码:

 <?php
header('Content-Type: text/html; charset=utf-8');
if (isset($_POST['email']))
//if "email" is filled out, send email
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent="<html><body>
<span style='background-color: #cdcdcd'>
text: $name
</span>
text: $phone
<span style='background-color: #cdcdcd'>
text: $email
</span>
text: $message </html><body/>";
$recipient = "email@email.com";
$subject = "text text text ";
$headers = "From: $email" . "\r\n" .
    "Reply-To: $email" . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
mail($recipient, $subject, $formcontent, $headers) or die("Error!");
echo "Thank You!";
?>

问题在于,我收到了电子邮件,我看到了电子邮件正文中的所有HTML标记,而且电子邮件没有造型。

我的问题是我该如何解决?

3 个答案:

答案 0 :(得分:3)

您实际上并未在电子邮件标头变量中设置Content-type标头

$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion() . "\r\n" .
"Content-Type: text/html; charset=utf-8";

在连接字符串

时,也可​​以使用单引号或双引号

答案 1 :(得分:0)

$headers而非text/html

需要header('Content-Type: text/html; charset=utf-8');

如果你没有得到更好的答案,我会在以后改进我的。目前时间不够。

答案 2 :(得分:0)

试一试 -

删除行

header('Content-Type: text/html; charset=utf-8');

并替换

$headers = "From: $email" . "\r\n" .
            "Reply-To: $email" . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

通过

$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
            "Reply-To: $email" . "\r\n" .
            'X-Mailer: PHP/' . phpversion();