Error sending email with PHP, Ajax and JSON

0

I have a simple form in PHP where the request is made through a JavaScrit with ajax that calls the PHP file and returns a response in JON. But the answer I have is always negative, according to the tests I'm doing it seems to me something with the mail() function of PHP.

The form:

<form role="form" id="footer-form" class="margin-clear">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group has-feedback mb-10">
                <label class="sr-only" for="name2">Nome</label>
                <input type="text" class="form-control" id="name2" placeholder="Seu nome" name="name2">
                <i class="fa fa-user form-control-feedback"></i>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group has-feedback mb-10">
                <label class="sr-only" for="email2">E-mail</label>
                <input type="email" class="form-control" id="email2" placeholder="Seu e-mail" name="email2">
                <i class="fa fa-envelope form-control-feedback"></i>
            </div>
        </div>
    </div>
    <div class="form-group has-feedback mb-10">
        <label class="sr-only" for="message2">Menssagem</label>
        <textarea class="form-control" rows="4" id="message2" placeholder="Sua menssagem" name="message2"></textarea>
        <i class="fa fa-pencil form-control-feedback"></i>
    </div>
    <input type="submit" value="Enviar" class="margin-clear submit-button radius-50 btn btn-default">
</form>

JavaScript:

if($("#footer-form").length>0) {
        $("#footer-form").validate({
            submitHandler: function(form) {
                $('.submit-button').button("loading");
                $.ajax({
                    type: "POST",
                    url: "php/email-sender.php",
                    data: {
                        "name": $("#footer-form #name2").val(),
                        "email": $("#footer-form #email2").val(),
                        "subject": "Mensagem do formulario de contato",
                        "message": $("#footer-form #message2").val()
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.sent == "yes") {
                            $("#MessageSent2").removeClass("hidden");
                            $("#MessageNotSent2").addClass("hidden");
                            $(".submit-button").removeClass("btn-default").addClass("btn-success").prop('value', 'Enviado');
                            $("#footer-form .form-control").each(function() {
                                $(this).prop('value', '').parent().removeClass("has-success").removeClass("has-error");
                            });
                        } else {
                            $("#MessageNotSent2").removeClass("hidden");
                            $("#MessageSent2").addClass("hidden");
                        }
                    }
                });
            },
            errorPlacement: function(error, element) {
                error.insertAfter( element );
            },
            onkeyup: false,
            onclick: false,
            rules: {
                name2: {
                    required: true,
                    minlength: 2
                },
                email2: {
                    required: true,
                    email: true
                },
                message2: {
                    required: true,
                    minlength: 10
                }
            },
            messages: {
                name2: {
                    required: "Especifique seu nome",
                    minlength: "Seu nome deve ter mais de 2 caracteres"
                },
                email2: {
                    required: "Precisamos do seu endereço de e-mail para entrar em contato com você",
                    email: "Insira um endereço de e-mail válido, ex. [email protected]"
                },
                message2: {
                    required: "Por favor, digite uma mensagem",
                    minlength: "Sua mensagem deve ter mais de 10 caracteres"
                }
            },
            errorElement: "span",
            highlight: function (element) {
                $(element).parent().removeClass("has-success").addClass("has-error");
                $(element).siblings("label").addClass("hide");
            },
            success: function (element) {
                $(element).parent().removeClass("has-error").addClass("has-success");
                $(element).siblings("label").removeClass("hide");
            }
        });
    };

The PHP file:

<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');

$Recipient = '[email protected]'; // <-- Set your email here

if($Recipient) {

    $sent = mail($Recipient, "assunto teste", "conteudo teste", "cabeçalhos testes");

    if ($sent) {
        $emailResult = array ('sent'=>'yes');
    } else {
        $emailResult = array ('sent'=>'no');
    }
        echo json_encode($emailResult);

} else {

    $emailResult = array ('sent'=>'no');
    echo json_encode($emailResult);

}
?>
    
asked by anonymous 28.05.2017 / 00:28

0 answers