1

HTMLメールを送信するためにこのPHPスクリプトを作成しました。ただし、電子メール出力は生のテキストとして表示され、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>

私は赤い箱を手に入れることを期待しています。私は何が間違っているのですか?

4

4 に答える 4

2

メールヘッダーを変更してみてください:content-typeそしてcharsetこのように:

$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";
于 2012-12-13T12:26:48.430 に答える
1
    $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";
于 2012-12-13T12:27:35.493 に答える
1

の代わりにを使用する必要が $headers .= "Content-type: text/html; charset=iso-8859-1". "\r\n"; あります$headers = "MIME-Version: 1.0\r\n";

$headers。="Content-type:text / html; charset = iso-8859-1rn"; $ headers = "MIME-Version: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>
于 2012-12-13T12:27:44.323 に答える
0

これが私のために働くものです:

$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);
于 2012-12-13T12:26:29.823 に答える