使用ajax和php成功发送消息但没有发送电子邮件

时间:2016-03-31 13:04:19

标签: php jquery ajax

我找到了一个模板,一切正常,但我不知道发生了什么...... 这是HTML代码



<form action="#" id="contactform" class="animform"><!-- Contact form -->
  <ul>
    <li>
      <label for="name" class="flabel">Name</label>
      <input type="text" name="name" value="" id="name" />
      <div class="indicate-name"></div>
    </li>
    <li>
      <label for="email" class="flabel">E-mail</label>
      <input type="text" name="email" value="" id="email" />
      <div class="indicate-email"></div>
    </li>
    <li>
      <label for="message" class="flabel">Enter your message</label>
      <textarea name="message" cols="88" rows="6" id="message"></textarea>
      <div class="indicate-message"></div>
    </li>
    <li>
      <button type="submit" name="submit" value="Send your message" class="submit">Send your message</button>
    </li>
  </ul>
</form><!-- /Contact form -->
&#13;
&#13;
&#13;

和这里的jQ代码

&#13;
&#13;
$(function () {
    'use strict';
    (function () {
        var MIN_NAME_LENGTH = 2,
            MIN_TEXT_LENGTH = 5,
            NAME_ERROR_TEXT = 'Minimum 2 characters',
            EMAIL_ERROR_TEXT = 'Please enter correct e-mail',
            MSG_ERROR_TEXT = 'Minimum 5 characters',
            ERROR_CLASS_NAME = 'error',
            SUCCESS_CLASS_NAME = 'ok',

            $contactForm = $('#contactform'),
            $formSuccess = $('.form-success'),
            $nameField = $contactForm.find('#name'),
            $emailField = $contactForm.find('#email'),
            $textField = $contactForm.find('#message');

        function init() {
            _bindEvents();
        }

        function _bindEvents() {
            $('.new-message').click(function() {
                $contactForm.delay(600).slideDown(1000);
                $formSuccess.slideUp(500);
            });

            $nameField.live('blur', _nameValidate);
            $emailField.live('blur', _emailValidate);
            $textField.live('blur', _textValidate);

            $contactForm.live('submit', function () {
                var status = _nameValidate(true) & _emailValidate(true) & _textValidate(true);
                if (!!status) {
                    _submitForm();
                }
                return false;
            });
        }

        function _submitForm() {
			var data = {
					name: $("#form_name").val(),
					email: $("#form_email").val(),
					message: $("#msg_text").val()
				};
            $.ajax({
                type: "post",
                url: "contact.php",
                data:{
                    'name': $nameField.val(),
                    'email': $emailField.val(),
                    'message': $textField.val()
                },
                success: function (msg) {
                    if (msg === 'SEND') {
                        $contactForm.slideUp(1000);
                        $formSuccess.delay(1000).slideDown(500);
                        setTimeout( function() {
                            // clear form value, shown labels
                            $nameField.val('');
                            $emailField.val('');
                            $textField.val('');
                            $contactForm.find( 'label[for="'+$nameField.attr( 'id' )+'"]').css( 'display', 'block').css( 'opacity', 1 );
                            $contactForm.find( 'label[for="'+$emailField.attr( 'id' )+'"]').css( 'display', 'block').css( 'opacity', 1 );
                            $contactForm.find( 'label[for="'+$textField.attr( 'id' )+'"]').css( 'display', 'block').css( 'opacity', 1 );
                        }, 1000 );
                    }
                    else {
                        $contactForm.prepend( '<div class="error">' + msg + '</div>' );
                    }
                },
                error: function( t, errorStatus ) {
                    $contactForm.prepend( '<div class="error">' + errorStatus + '</div>' );
                },
                beforeSend: function() {
                    $(".error,.success").remove();
                }
            });
        }

        function _nameValidate(errIfEmpty) {
            var $memo = $contactForm.find('.indicate-name'),
                val = $nameField.val().replace(/\s+$/g, ''),
                result = false;
            errIfEmpty = errIfEmpty === true ? true : false;

            if (!errIfEmpty && val.length === 0) {
                $memo
                    .text('')
                    .removeClass(SUCCESS_CLASS_NAME)
                    .removeClass(ERROR_CLASS_NAME);
            } else {
                if (val.length >= MIN_NAME_LENGTH) {
                    $memo
                        .text('')
                        .removeClass(ERROR_CLASS_NAME)
                        .addClass(SUCCESS_CLASS_NAME);
                    result = true;
                } else {
                    $memo
                        .text(NAME_ERROR_TEXT)
                        .removeClass(SUCCESS_CLASS_NAME)
                        .addClass(ERROR_CLASS_NAME);
                }
            }
            return result;
        }

        function _emailValidate(errIfEmpty) {
            var $memo = $contactForm.find('.indicate-email'),
                val = $emailField.val().replace(/\s+$/g, ''),
                regExp = /^.+@.+\..{2,6}$/i,
                result = false;
            errIfEmpty = errIfEmpty === true ? true : false;

            if (!errIfEmpty && val.length === 0) {
                $memo
                    .text('')
                    .removeClass(SUCCESS_CLASS_NAME)
                    .removeClass(ERROR_CLASS_NAME);
            } else {
                if (regExp.test(val)) {
                    $memo
                        .text('')
                        .removeClass(ERROR_CLASS_NAME)
                        .addClass(SUCCESS_CLASS_NAME);
                    result = true;
                } else {
                    $memo
                        .text(EMAIL_ERROR_TEXT)
                        .removeClass(SUCCESS_CLASS_NAME)
                        .addClass(ERROR_CLASS_NAME);
                }
            }
            return result;
        }

        function _textValidate(errIfEmpty) {
            var $memo = $contactForm.find('.indicate-message'),
                val = $textField.val().replace(/\s+$/g, ''),
                result = false;
            errIfEmpty = errIfEmpty === true ? true : false;

            if (!errIfEmpty && val.length === 0) {
                $memo
                    .text('')
                    .removeClass(SUCCESS_CLASS_NAME)
                    .removeClass(ERROR_CLASS_NAME);
            } else {
                if (val.length >= MIN_TEXT_LENGTH) {
                    $memo
                        .text('')
                        .removeClass(ERROR_CLASS_NAME)
                        .addClass(SUCCESS_CLASS_NAME);
                    result = true;
                } else {
                    $memo
                        .text(MSG_ERROR_TEXT)
                        .removeClass(SUCCESS_CLASS_NAME)
                        .addClass(ERROR_CLASS_NAME);
                }
            }
            return result;
        }

        init();
    })();
});
&#13;
&#13;
&#13;

这是php代码:

<?php 

    $to  =   'myEmail@anyEmail.com'; 
    $from = $_POST['email']; 
    $name = $_POST['name'];
    $subject = "Message from site";
    $theMessage = $_POST['message'];

    $message = "This message from \n\n Name: " . $name . "\n\n";
    $message .= "Enquiries: " . $theMessage . "\n\n";

    $headers  = "From: $from\r\n";
    $headers .= "Reply-To: $from\r\n";

    if (mail($to, $subject, $message, $headers)) { 
        echo "SEND";
    }else{
        echo "Error";
    }

//print_r(error_get_last());?>

表单确实说成功,但仍然没有收到电子邮件!任何想法?

P.S。我尝试了两种不同的电子邮件&#34; hotmail&#34;和&#34; ymail&#34;,但他们两个没有收到任何东西.. 谢谢

1 个答案:

答案 0 :(得分:0)

发送电子邮件trought php需要一个smtp配置。 mail()函数如果成功命令系统发送邮件,则返回true。 但是如果你的smtp有问题,邮件永远不会到达。 然后,您必须正确格式化标题http://php.net/manual/fr/function.mail.php