How to restrict execution of sendmail.php to only one page from within the same server?

Solution for How to restrict execution of sendmail.php to only one page from within the same server?
is Given Below:

I have 2 files in a server:

https://example.com/form.htm which contains an ajax function to send an email via POST to a sendmail.php file:

//html form code ommited

$(document).ready(function() {
    $("#myForm").on('submit', function(event) { 
        event.preventDefault(); 
        var formData = $(this).serialize();
        $.ajax({
            type: 'POST',
            url: 'sendmail.php',
            dataType: "json",
            data: formData,
            success: function(response) { 
                alert(response.success); 
            },
            error: function(xhr, status, error){
                console.log(xhr); 
            }
        });
    });
});

and a sendmail.php file, which sends the email via PHP’s mail()function:

if(isset($_POST['message'])){
    $to      = '[email protected]';
    $subject = $_POST['subject']; 
    $message = $_POST['message']; 
    $headers = "From: ".$_POST['sender_nam‌​e​']." <".$_POST['sender_em‌​ail‌​'].">rn"; $headers = "Reply-To: ".$_POST['sender_ema‌​il‌​']."rn"; 
    $headers = "Content-type: text/html; charset=iso-8859-1rn";
    'X-Mailer: PHP/' . phpversion();
    if(mail($to, $subject, $message, $headers)) echo json_encode(['success'=>true]); 
    else echo json_encode(['success'=>false]);
    exit;
 }

And it works, however i want to secure the execution of the mail() code to only work from within my server, and to avoid it to work from an unauthorized form (for example, if a form in another server or local host makes a POST request to my sendmail.php file).
What is the best way to do it? I was thinking on including a validation to the script against the database (so that it executes only if the request comes with a valid user, however in some instances a valid request can come from an unregistered user). Thanks in advance.