邮件未到达我的电子邮件ID

时间:2013-11-28 18:40:09

标签: php jquery ajax

我的联系页面中有此代码。

<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/main.js"></script>
<script>
jQuery(document).ready(function() {

        $("#submit").click(function() 
        {   
            var pattern = /^[a-zA-Z0-9\-_]+(\.[a-zA-Z0-9\-_]+)*@[a-z0-9]+(\-[a-z0-9]+)*(\.[a-z0-9]+(\-[a-z0-9]+)*)*\.[a-z]{2,4}$/;
            //var pn = /^(\+91-|\+91|0)?\d{10}$/;


            var email = $("#email").val();
            var message = $("#message").val();
            var subject = $("#subject").val();
            var name = $("#name").val();
            if(name=="")
            {
                //$('#empty1').show(1).delay(5000).fadeOut();
                $('#name').focus();
                return false;
            }
            else if(message=="")
            {
                //$('#empty4').show(1).delay(5000).fadeOut();
                $('#message').focus();
                return false;
            }
            else if(subject=="")
            {
                //$('#empty4').show(1).delay(5000).fadeOut();
                $('#subject').focus();
                return false;
            }
            else if(!(pattern.test(email)))
            {
                //$('#error2').show(1);
                $('#email').focus();
            }
            else if(email=="")
            {
                //$('#empty2').show(1).delay(5000).fadeOut();
                 $('#email').focus();
                return false;
            }
            else
            {
        var dataString = 'name='+ name + '&email=' + email + '&message=' + message + '&subject=' + subject;
                $.ajax({
                    type: "POST",
                    url: "mail.php",
                    data: dataString,
                    success: function(){
                        //$('.success').show('slide').delay(5000).fadeOut();
                        $("#contact-form")[0].reset();
                        alert("Your Detail Is Submitted, We Will Connect With You Soon.");
                    }
                });
            }
            return false;
        });

});

我有这样的形式。

<form class="b-form b-contact-form m-contact-form" action="" style="margin-bottom: 10px;" id="contact-form">
            <div class="input-wrap">
                <i class="icon-user"></i>
                <input class="field-name" type="text" placeholder="Name (required)" name="name" id="name">
            </div>
            <div class="input-wrap">
                <i class="icon-envelope"></i>
            <input class="field-email" type="text" placeholder="Email (required)" name="email" id="email">
            </div>
            <div class="input-wrap">
                <i class="icon-pencil"></i>
                <input class="field-subject" type="text" placeholder="Subject" name="suject" id="subject">
            </div>
            <div class="textarea-wrap">
                <i class="icon-pencil"></i>
            <textarea class="field-comments" placeholder="Message" name="message" id="message"></textarea>
            </div>
        <input class="btn-submit btn colored" type="submit" value="Submit Comment" id="submit" name="submit">
        </form>

我也有ajax mail.php页面。

<?php
$to = "demo@example.com";
$subject = $_REQUEST["subject"];
$message = "message=".$_REQUEST["message"]."<br />";
$message .= "name=".$_REQUEST["name"]."<br />";
$message .= "email=".$_REQUEST["email"];
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From:" .$_REQUEST["email"]. "\r\n";
mail($to,$subject,$message,$headers);
?>

我完成了服务器邮件转发设置,还检查了ajax调用,以及它将解析哪些数据。使用控制台apnel。 但我无法通过电子邮件ID到达邮件。 请帮助我,我现在能做什么?

1 个答案:

答案 0 :(得分:1)

在这里,试一试。

您的消息变量格式不正确,我稍微修改了您的标题。

这对我有用,减去你的jQuery方法。

<?php
$to = "demo@example.com";
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$message .= "" . "<br/>";
$message .= "name= $name" . "<br/>";
$message .= "email= $email" . "<br/>";

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

$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();

if(mail($to,$subject,$message,$headers)){
echo "Success!!";
}
else {
echo "Sorry.";
}
?>