0

I'm unable to send mail on my shared server using ajax and php, is there a problem with the php mail configuration on the server, i'm using hostgator i have tried in my local ubuntu machine also but there also the same error as well as how can i get more detail on this error the error i'm getting is


Error in mail config

my code

<script type="text/javascript">
$(document).ready(function () {
        $("#submit_btn").click(function () {
            var proceed = true;
            $("#contact_form input[required=true], #contact_form textarea[required=true]").each(function () {
                $(this).css('border-color', '');
                if (!$.trim($(this).val())) { //if this field is empty
                    $(this).css('border-color', 'red'); //change border color to red
                    proceed = false; //set do not proceed flag
                }
                var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if ($(this).attr("type") == "email" && !email_reg.test($.trim($(this).val()))) {
                    $(this).css('border-color', 'red'); //change border color to red
                    proceed = false; //set do not proceed flag
                }
            });
            if (proceed) //everything looks good! proceed...
            {
                post_data = {
                    'user_name': $('input[name=name]').val(),
                    'user_email': $('input[name=email]').val(),
                    'msg': $('textarea[name=message]').val()
                };
                $.post('contact/contact.php', post_data, function (response) {
                    if (response.type == 'error') { //load json data from server and output message
                        output = '<div class="error">' + response.text + '</div>';
                    } else {
                        output = '<div class="success">' + response.text + '</div>';
                        //reset values in all input fields
                        $("#contact_form input[required=true], #contact_form textarea[required=true]").val("");
                        $("#contact_form #contact_body").slideUp(); //hide form after success
                        window.setTimeout(function () { location.reload() }, 3000);
                    }
                    $("#contact_form #contact_results").hide().html(output).slideDown();
                }, 'json');
            }
        });
        $("#contact_form input[required=true], #contact_form textarea[required=true]").keyup(function () {
            $(this).css('border-color', '');
            $("#contact_results").slideUp();
        });
    });
</script>

and my php code

<?php
if($_POST)
{$to_email = "info@example.com"; //Recipient email, Replace with own email here

//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

    $output = json_encode(array( //create JSON data
        'type'=>'error', 
        'text' => 'Sorry Request must be Ajax POST'
    ));
    die($output); //exit script outputting json data
}

//Sanitize input data using PHP filter_var().
$user_name      = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
$user_email     = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
$message        = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);

//additional php validation
if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error.
    $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
    die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
    $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
    die($output);
}
if(strlen($message)<3){ //check emtpy message
    $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
    die($output);
}

//email body
$message_body = $message."\r\n\r\n-".$user_name."\r\nEmail : ".$user_email."\r\nEmail : ". $user_email;
$subject = "Mail From website";

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

$send_mail = mail($to_email, $subject, $message_body, $headers);

if(!$send_mail)
{
    //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
    $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
    die($output);
}else{
    $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email'));
    die($output);
}
}
?>
Chetan Naik
  • 151
  • 13
  • Check error by using `print_r($send_mail);` at before `$send_mail()` calling. Let me know what is an error? – AddWeb Solution Pvt Ltd Jan 12 '16 at 05:28
  • Also, Php default mail function is not always reliable, you should try phpmailer library – user4804138 Jan 12 '16 at 05:28
  • Use PHPMailer, trust me. It quick to learn and you can avoid all the bs you are going through. http://phpmailer.worxware.com/?pg=tutorial. – Naterade Jan 12 '16 at 05:29
  • thank you guys the problem is solved but i have a doubt if i use recipient email as same the URL for example if my website is http://example.com the i'm able to send to info@example.com i'm not able to send to gmail or any other professional email rather than this – Chetan Naik Jan 12 '16 at 05:36
  • the problem was with the recipient email, i have tried using gmail and other professional email address which didn't work – Chetan Naik Jan 12 '16 at 05:39
  • many time PHP mail function doesn't work on local server. Where did you script is been Local server or live server? – Ajay Makwana Jan 12 '16 at 06:46
  • i have tried on both local as well as on live server the issue was with the recipient mail – Chetan Naik Jan 12 '16 at 06:56

0 Answers0