【问题标题】:PHP script to send html email用于发送 html 电子邮件的 PHP 脚本
【发布时间】:2012-12-13 12:24:23
【问题描述】:

我编写了这个 PHP 脚本来发送 HTML 电子邮件。但是,电子邮件输出显示为原始文本,而不是被解释为 html。

<?php 
 if(isset($_POST['submit']))  {
    $to = $_POST['email'];
    $from = "info@test.com";
    $subject = "subject";
    $message = "<div style=\"background:red;height:100px; " .
      "width:100px;display:block;\">dfsdf</div>";

    $headers = "MIME-Version: 1.0rn";
    $headers .= "Content-type: text/html; charset=iso-8859-1rn";
    $headers  .= "From: $from\r\n";
    mail($to, $subject, $message, $headers);
    echo "Message has been sent....!";  
 }else{
    echo "Add an email address"; 
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="/send" method="post">
  <input name="email" type="text" />
  <input name="submit" type="submit" value="Submit">
</form>
</body>
</html>

我收到的电子邮件是:

<div style="background:red;height:100px;width:100px;display:block;">dfsdf</div>

我希望得到一个红色的盒子。我做错了什么?

【问题讨论】:

  • 电子邮件的显示方式完全取决于您的电子邮件阅读器。它应该与这段代码无关。

标签: php html html-email


【解决方案1】:

尝试更改电子邮件标题:content-typecharset,如下所示:

$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";

$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";

【讨论】:

    【解决方案2】:

    你必须使用$headers .= "Content-type: text/html; charset=iso-8859-1". "\r\n";$headers = "MIME-Version: 1.0\r\n"; 而不是

    $headers .= "内容类型: text/html; charset=iso-8859-1rn"; $headers = "MIME 版本:1.0rn";

    所以试试这个

     <?php 
         if(isset($_POST['submit']))  {
            $to = $_POST['email'];
            $from = "info@test.com";
            $subject = "subject";
            $message = "<div style=\"background:red;height:100px;width:100px;display:block;\">dfsdf</div>";
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1". "\r\n";
            $headers  .= "From: $from\r\n";
            mail($to, $subject, $message, $headers);
            echo "Message has been sent....!";  
         }else{
            echo "Add an email address"; 
         }?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        </head>
        <body>
        <form action="/send" method="post">
          <input name="email" type="text" />
          <input name="submit" type="submit" value="Submit">
        </form>
        </body>
        </html>
    

    【讨论】:

      【解决方案3】:
          $mail_to=$tomail;
      
          $mail_from=$frommail;
      
          $mail_sub="Subject";
      
          $mail_mesg="Body of your Message";
      
          $headers = "MIME-Version: 1.0 \r\n";
      
          $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
      
          $headers  .= "From: $frommail\r\n";
      
          if(mail($mail_to,$mail_sub,$mail_mesg,$headers))
              $result = "send successfully";
          else
              $result = "failed to send mail";
      

      【讨论】:

        【解决方案4】:

        这对我有用:

        $mailheaders = "From: $email\n";
        $mailheaders .= "Reply-to: $email\n";
        $mailheaders .= "Content-Type: text/html; charset=iso-8859-1\n";
        
        mail('my@email.com', 'Test', 'Foo bar', $mailheaders);
        

        【讨论】:

          猜你喜欢
          • 2018-09-06
          • 2011-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-12
          • 2011-04-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多