dongyi0114 2015-01-11 04:46
浏览 32
已采纳

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

I am able to send plain text emails from my contact form using php, but I am not able to send the content as HTML. Thought I had added the headers correctly, but apparently there is still a problem.

This is the script I am using:

<?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' . "
";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

                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条回答 默认 最新

  • doulai5585 2015-01-11 04:58
    关注

    try this. code is un-tested. and it's been eons since i've written in php so i could have easily mangled some syntax in there.

    $tacos   = "crunchy";
    
    $to      = "tacos4eva@email.com"; 
    $subject = "tacos"; 
    $mail    = "the tacos are $tacos today."; 
    
    $headers  = 'MIME-Version: 1.0' . "
    ";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
    ";
    $headers .= 'From: taco_master@email.com' . "
    ";
    
    $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 the page...

    When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.

    <?php
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "
    " .
        'Reply-To: webmaster@example.com' . "
    " .
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题