What are the best practices for sending form data from a jQuery calculation form to a PHP script via email?

When sending form data from a jQuery calculation form to a PHP script via email, it is important to properly sanitize and validate the input data to prevent any security vulnerabilities. Additionally, the form data should be formatted correctly before sending it via email. One common method is to use AJAX to send the form data to a PHP script that processes the data and sends an email.

<?php
// Get form data sent via POST
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

// Validate email address
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Invalid email format";
    exit;
}

// Set up email parameters
$to = 'recipient@example.com';
$subject = 'Form Submission';
$headers = 'From: ' . $email;

// Compose email message
$body = "Name: $name\n";
$body .= "Email: $email\n";
$body .= "Message: $message\n";

// Send email
if (mail($to, $subject, $body, $headers)) {
    echo "Email sent successfully";
} else {
    echo "Email sending failed";
}
?>