如何在使用php mail()函数发送的邮件中显示正确的html输出?

时间:2014-03-18 09:57:48

标签: php ajax email html-table

我忘了密码表。

我正在使用电子邮件ID发送邮件。

HTML code:

<form action="#" method="post">
     <table border="0" width="" id="forgot_password">
         <tr>
             <td>Enter Email ID:</td>
             <td><input type="email" name="email_id" id="email_id"/></td>
         </tr>
         <tr>
             <td colspan="2"><span class="message validation"></span></td>
         </tr>
         <tr>
             <td></td>
             <td><input type="button" name="forgot" class="myButton" value="Submit"/></td>
         </tr>
     </table>
</form>

单击忘记按钮我将ajax函数称为:

jQuery(function($) {
        var val_holder;

        $("form input[name='forgot']").click(function() {
            var email = jQuery.trim($("form input[name='email_id']").val());
            var email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

            if(email == "") {
                alert("Please Enter Email ID");
                return false;
            }
            if(email != "") {
                if(!email_regex.test(email)){ // if invalid email
                    $("span.message").html("Your email id is invalid.");
                }
            }

            $("span.validation").html("");

            var datastring = 'email='+ email;

            $.ajax({
                type: "POST", // type
                url: "forgot_password_details.php", // request file the 'check_email.php'
                data: datastring, // post the data
                success: function(responseText) 
                {   // get the response
                    if(responseText == 1) { // if the response is 1
                        $("span.message").html("<img width='15' height='15' src='image/delete_agent.png'> Email id doesn't exists.");
                    }
                    else{
                        $("span.message").html("<img width='15' height='15' src='image/correct.png'> Sent mail to you.");
                    }
                } // end success
            });
        });
    });

forgot_password_details.php我正在检查邮件是否存在。

如果数据库中存在邮件,则将邮件发送到该电子邮件ID:

forgot_password_details.php

<?php
   $uemail = $_POST['email'];

    $user_emailid = mysql_query("SELECT * FROM user_details WHERE email_id = '$uemail'");

    if(mysql_num_rows($user_emailid) == 1){
        $row = mysql_fetch_array($user_emailid);

        $email_id = $row['email_id'];
        $username = $row['uname'];
        $password = $row['upassword'];

        $to      = $email_id;
        $subject = 'login credentials';
        $message = "<html>
                        <body>
                            <table>
                                <tr>
                                    <td>Login credentials:</td>
                                    <td></td>
                                </tr>
                                <tr>
                                    <td>Username:</td>
                                    <td>$username</td>
                                </tr>
                                <tr>
                                    <td>Password</td>
                                    <td>$password</td>
                                </tr>
                            </table>
                        </body>
                    </html>";

                    //echo $message; die;

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-Type: text/html; charset=iso-8859-1' . "\r\n";
        $headers .= "From: Fairmacs";
        mail($to, $subject, $message, $header);
    }
    else{
        echo '1';
    }
?>

我收到了邮件,但html正在打印,因为它是邮件。

因此,如我们在浏览器中显示的那样,如何以用户可读的形式打印它。

请帮我解决这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

请在$ header下面尝试

$boundary = "nextPart";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: Me <youeemail>\r\n";
$headers .= "Content-Type: multipart/alternative; boundary = $boundary\r\n";
//text version
$headers .= "\n--$boundary\n"; // beginning \n added to separate previous content
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "This is the plain version";
//html version
$headers .= "\n--$boundary\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "This is the <b>HTML</b> version";

而不是

 $headers = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";