PHP mail()没有发送,但代码说它已被发送

时间:2016-10-19 12:05:49

标签: javascript php email contact-form

我的联系表格有点问题。 我的代码说它已发送但我在收件箱中没有收到任何内容。

我的代码可以在下面找到:

HTML:

<form name="contactForm" id='contact_form' method="post" action='inc/email.php'>
            <div class="form-inline">
              <div class="form-group col-sm-6">
                <input type="text" class="form-control" name="name" id="exampleInputName" placeholder="naam" >
              </div>
              <div class="form-group col-sm-6">
                <input type="email" class="form-control" name="email" id="exampleInputEmail" placeholder="E-mail adres">
              </div>
              <div class="form-group col-sm-6">
                <input type="text" class="form-control" name="subject" id="exampleInputSubject" placeholder="Onderwerp" >
              </div>
              <div class="form-group col-sm-6">
                <input type="text" class="form-control" name="company" id="exampleInputCompany" placeholder="Bedrijfsnaam" >
              </div>
              <div class="form-group col-sm-12">
                <textarea class="form-control" name="message" rows="3" id="exampleInputMessage" placeholder="Bericht" ></textarea>
              </div>
            </div>
            <div class="form-group col-xs-12">
              <div id='mail_success' class='success' style="display:none;">Uw bericht is succesvol verzonden!
              </div><!-- success message -->
              <div id='mail_fail' class='error' style="display:none;"> Sorry, er is een fout opgetreden tijdens het verzend van uw bericht. Probeer het later opnieuw.
              </div><!-- error message -->
            </div>
            <div class="form-group col-sm-12" id='submit'>
              <input type="submit" name="submit" id='send_message' class="btn  btn-lg costom-btn" value="Verzend">
            </div>
        </form>

这是PHP代码:

error_reporting(E_ALL); 
session_cache_limiter( 'nocache' );
$subject = $_REQUEST['subject']; // Subject of your email
$to = "my e-mail";  //Recipient's E-mail

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $_REQUEST['name'].'<'.$_REQUEST['email'] .'>'. "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$message  = 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= 'Company: ' . $_REQUEST['company'] . "<br>";
$message .= $_REQUEST['message'];

if (mail($to, $subject, $message, $headers))
{
    // Transfer the value 'sent' to ajax function for showing success message.
    echo "sent";
}
else
{
    // Transfer the value 'failed' to ajax function for showing error message.
    echo 'failed';
}

这是JS:

$(document).ready(function(){
    $('#send_message').click(function(e){

        // Stop form submission & check the validation
        e.preventDefault();

        // Variable declaration
        var error = false;

        var email = $('#exampleInputEmail').val();
        var subject = $('#exampleInputSubject').val();
        var message = $('#exampleInputMessage').val();


        if(email.length == 0 || email.indexOf('@') == '-1'){
            var error = true;
            // $('#email_error').fadeIn(500);
            $('#exampleInputEmail').addClass("validation");

        }else{
            $('#exampleInputEmail').removeClass("validation");
        }
        if(subject.length == 0){
            var error = true;
            $('#exampleInputSubject').addClass("validation");
        }else{
            $('#exampleInputSubject').removeClass("validation");
        }
        if(message.length == 0){
            var error = true;
            $('#exampleInputMessage').addClass("validation");
        }else{
            $('#exampleInputMessage').removeClass("validation");
        }

        // If there is no validation error, next to process the mail function
        if(error == false){
           // Disable submit button just after the form processed 1st time successfully.

            $('#send_message').attr({'enabled' : 'enable', 'value' : 'Verzenden...' });

            /* Post Ajax function of jQuery to get all the data from the submission of the form as soon as the form sends the values to email.php*/
            $.post("inc/email.php", $("#contact_form").serialize(),function(result){
                //Check the result set from email.php file.
                if(result == 'sent'){
                    //If the email is sent successfully, remove the submit button

                     $('#send_message').attr({'enabled' : 'enable', 'value' : 'send' });
                    //Display the success message
                    $('#mail_success').fadeIn(500);
                }else{
                    //Display the error message
                    $('#mail_fail').fadeIn(500);
                    // Enable the submit button again
                    $('#send_message').removeAttr('disabled').attr('value', 'Send The Message');
                }
            });
        }
    });    
});

我希望有人可以查看我的代码并发现导致此问题的错误。

提前致谢!

亲切的问候。

0 个答案:

没有答案