PHP - 电子邮件不以HTML格式发送。为什么?

时间:2015-01-11 04:46:27

标签: php html email header

我可以使用php从我的联系表单发送纯文本电子邮件,但我无法将内容作为HTML发送。以为我已经正确添加了标题,但显然仍然存在问题。

这是我正在使用的脚本:

<?php


    $to = 'test@hotmail.com'; 
    $subject = 'From your Web';
    $email = $_REQUEST['email'] ;


    if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
    //if "email" is filled out, send email
        if (!trim($_REQUEST['name']) == "" ) 
        {
            if (!trim($_REQUEST['message']) == "" ) 
            {

                //send email
                $name = $_REQUEST['name'] ;
                $message = $_REQUEST['message'] ;
                $mail = '
                    <html>
                        <body>
                             <table border=1>
                            <tr>
                                <td>Name:</td>
                                <td>'.$name.' </td>
                            </tr>
                            <tr>
                                <td>Email:</td>
                                <td>'.$email.'</td>
                            </tr>
                            <tr>
                                <td>Msg:</td>
                                <td>'.$message.'</td>
                            </tr>
                        </table>
                        </body>
                        </html>
                ';

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

                mail($to, $subject, $mail, "From:" . $email, $headers);
                //mail($to, $subject, "From: " . $name . "/" . $email . " Msg: ".$message, "From:" . $email);
                echo 'Thank you!';

            }
            else{

                echo 'No empty msg';

            }   
        }
        else{

            echo 'This is not a name';
        }
    }
    else{

        echo 'No correct email';
    }

    ?>

1 个答案:

答案 0 :(得分:2)

试试这个。代码未经测试。自从我用PHP编写以来,它已经很久了,所以我可以很容易地在那里修改一些语法。

$tacos   = "crunchy";

$to      = "tacos4eva@email.com"; 
$subject = "tacos"; 
$mail    = "the tacos are $tacos today."; 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: taco_master@email.com' . "\r\n";

$success = mail($to, $subject, $mail, $headers); 

if ($success) echo "tacos were sent successfully"; 
         else echo "tacos fell on the floor"; 

http://php.net/manual/en/function.mail.php

来自页面...

  

发送邮件时,邮件必须包含From标头。这可以   使用additional_headers参数设置,或者可以设置默认值   php.ini中。

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>