Error sending email with PHP, Ajax and JSON

Asked

Viewed 109 times

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 always have is negative, according to the tests I’m doing seems something with the function mail() 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>

O 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);

}
?>
  • If your problem is just mail(), then you don’t need to show any of the other code... Did you test it as a simple PHP file? You checked the messages in the server error_log?

  • tested a simple file in php, in 3 different domains, the 3 returned me the same error (internal error on the server), as the domains are hosted on the web, I do not have the logs, however I will request... thanks.

  • This should be in cPanel of the hosting service. Check this also: Debugging - Wordpress - Stack Overflow

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.