【问题标题】:Remove the HTML tags from the email, and desing the email body [duplicate]从电子邮件中删除 HTML 标签,并设计电子邮件正文 [重复]
【发布时间】:2013-09-04 15:27:18
【问题描述】:

我有以下 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 标记,并且电子邮件没有样式。

我的问题是如何解决它?

【问题讨论】:

    标签: php html email html-email


    【解决方案1】:

    试试看 -

    删除线

    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();
    

    【讨论】:

      【解决方案2】:

      您实际上并未在电子邮件标头变量中设置 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";
      

      在连接字符串时也只使用单引号或双引号

      【讨论】:

      • 我会注意的,非常感谢!
      【解决方案3】:

      text/html 需要 $headers 而不是 header('Content-Type: text/html; charset=utf-8');

      如果你没有得到更好的答案,我稍后会改进我的。目前时间很紧。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-08
        • 1970-01-01
        • 2012-05-27
        • 1970-01-01
        相关资源
        最近更新 更多