使用PHP错误通过电子邮件发送联系表单

时间:2016-02-12 16:13:55

标签: php html email

发送打印成功后,我刚收到一条联系表格,但不发送电子邮件!!

//------------------------------------------------------------------------------------
//                      CONTACT FORM VALIDATION'S SETTINGS
//------------------------------------------------------------------------------------        
    $('#contact_form').validate({
        onfocusout: false,
        onkeyup: false,
        rules: {
            name: "required",
            message: "required",
            email: {
                required: true,
                email: true
            }
        },
        errorPlacement: function(error, element) {
            error.insertAfter(element);
        },
        messages: {
            name: "What's your name?",
            message: "Type your message",
            email: {
                required: "What's your email?",
                email: "Please, enter a valid email"
            }
        },
                    
        highlight: function(element) {
            $(element)
            .text('').addClass('error')
        },                    
                    
        success: function(element) {
            element
            .text('').addClass('valid')
        }
    });   




//------------------------------------------------------------------------------------
//                              CONTACT FORM SCRIPT
//------------------------------------------------------------------------------------  
    
    $('#contact_form').submit(function() {
        // submit the form
        if($(this).valid()){
            $('#contact_submit').button('loading'); 
            var action = $(this).attr('action');
            $.ajax({
                url: contact.php,
                type: 'POST',
                data: {
                    contactname: $('#contact_name').val(),
                    contactemail: $('#contact_email').val(),
                    contactmessage: $('#contact_message').val()
                },
                success: function() {
                    $('#contact_submit').button('reset');
                    $('#modalContact').modal('hide');
                    
                    //Use modal popups to display messages
                    $('#modalMessage .modal-title').html('<i class="icon ion-ios-checkmark"></i>Well done!<br>Your message has been successfully sent!');
                    $('#modalMessage').modal('show');
                },
                error: function() {
                    $('#contact_submit').button('reset');
                    $('#modalContact').modal('hide');
                    
                    //Use modal popups to display messages
                    $('#modalMessage .modal-title').html('<i class="icon ion-ios-close"></i>Oops!<br>Something went wrong!');
                    $('#modalMessage').modal('show');
                }
            });
        } else {
            $('#contact_submit').button('reset')
        }
        return false; 
    });           

});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form</title>
</head>
<body>
<form action="contact.php" role="form" id="contact_form">
    <div class="form-group">
        <input type="text" class="form-control" id="contact_name" placeholder="Full name" name="name">
    </div>
    <div class="form-group">
        <input type="email" class="form-control" id="contact_email" placeholder="Email Address" name="email">
    </div>
    <div class="form-group">
                <textarea class="form-control" rows="4" placeholder="Your message or question" id="contact_message"
                          name="message"></textarea>
    </div>
    <button type="submit" id="contact_submit" data-loading-text="&bull;&bull;&bull;"
            class="btn btn-block btn-fix btn-line">Send
    </button>
</form>
<script src="scripts.js"></script>
</body>
</html>

<?php
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) {
        $to = 'email@email.com'; // Your e-mail address here.
        $body = "\nName: {$_POST['name']}\nEmail: {$_POST['email']}\n\n\n{$_POST['message']}\n\n";
        mail($to, "Message from yoursite.com", $body, "From: {$_POST['email']}"); // E-Mail subject here.
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

我接受你的javascript正在运行。 PHP方可能是这样的。这很有效。

<?php
    if(isset($_POST['name'],$_POST['email'],$_POST['message'])) {
        if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message'])) {        
            $to = 'email@email.com'; // Your e-mail address here.
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
            $headers .= "From: " . $_POST['name'] . " <" . $_POST['email'] . ">" . "\r\n";
            $body = "\nName: {$_POST['name']}\nEmail: {$_POST['email']}\n\n\n{$_POST['message']}\n\n";
            ini_set("sendmail_from", $_POST['email']);
            mail($to, "Message from yoursite.com", $body, $headers);
        }
    }
    ?>