-1

I have previously used the same script and AJAX query to handle emails and they work fine, but for some reason, it is not working on this site. The POST request says it succeeds but no email is received at the other end.

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    # FIX: Replace this email with recipient email
    $mail_to = "sample@email.co.nz";
    
    # Sender Data
    $fname = str_replace(array("\r","\n"),array(" "," ") , strip_tags(trim($_POST["fname"])));
    $lname = str_replace(array("\r","\n"),array(" "," ") , strip_tags(trim($_POST["lname"])));
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
    $subject = trim($_POST["subject"]);
    $message = trim($_POST["message"]);

    if ( empty($fname) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($lname) OR empty($message) OR empty($subject)) {
        # Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Please complete the form and try again.";
        exit;
    }
    
    # Mail Content
    $content = "Name: $fname $lname\n";
    $content .= "Email: $email\n";
    $content .= "Message:\n$message\n";

    # email headers.
    $headers = "From: $fname $lname";

    # Send the email.
    $success = mail($mail_to, $subject, $content, $headers);
    if ($success) {
        # Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank you for your message! Someone will be in touch shortly.";
    } else {
        # Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Sorry, something went wrong and we couldn't send your message!";
    }

} else {
    # Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

?>

JQuery:

(function($) {
'use strict';

var form = $('#contact_form'),
    form_data;

form.submit(function(e) {
    e.preventDefault();
    form_data = $(this).serialize();
    $.ajax({
            type: 'POST',
            url: form.attr('action'),
            data: form_data
        })
        .done(done_func)
        .fail(fail_func);
});

})(jQuery);

The network tab of inspects shows that both the jquery and the PHP are returning fine, and it acts normally in every other way. The only differences between this and a working example are the actual HTML content but I see no reason as to why it wouldn't work in my HTML

Full Stop
  • 904
  • 11
  • 24
David McIntyre
  • 218
  • 1
  • 9

1 Answers1

0

try removing header parameter

link to php mail usage 
https://www.w3schools.com/php/func_mail_mail.asp
Hammad Ahmed khan
  • 1,618
  • 7
  • 19