简体   繁体   English

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

[英]Message successfully sent with ajax and php but no email

I found a template and everything works fine, but I don't know what is going on.. here is HTML code 我找到了一个模板,并且一切正常,但是我不知道发生了什么..这是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 --> 

and here's jQ code 这是jQ代码

 $(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(); })(); }); 

and this is the php code: 这是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());?>

The form does say success, but still not receiving the emails! 表格确实表示成功,但仍未收到电子邮件! any ideas? 有任何想法吗?

PS I tried two different Emails "hotmail" and "ymail", but the two of them didn't receive anything.. thanks PS我尝试了两个不同的电子邮件“hotmail”和“ymail”,但他们两个没有收到任何东西..谢谢

Send email trought php need a smtp configuration. 发送电子邮件trought php需要一个smtp配置。 The mail() function return true if he successfull order to the system to send the mail. 如果成功向系统发送邮件,mail()函数将返回true。 But if you have trouble with your smtp, the mail will never arrive. 但是如果你的smtp有问题,邮件永远不会到达。 Then you will have to format your headers correctly http://php.net/manual/fr/function.mail.php 然后你必须正确格式化你的标题http://php.net/manual/fr/function.mail.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM