File: /var/www/html/kollect/wp-content/themes/kollect/functions.php
<?php
add_action( 'wp_ajax_custom_contact_form_ajax', 'handle_custom_contact_form_ajax' );
add_action( 'wp_ajax_nopriv_custom_contact_form_ajax', 'handle_custom_contact_form_ajax' );
function handle_custom_contact_form_ajax() {
// Sanitize fields
$name = sanitize_text_field( $_POST['name'] );
$email = sanitize_email( $_POST['emailid'] );
$country = sanitize_text_field( $_POST['country_code'] );
$phone = sanitize_text_field( $_POST['phone_number'] );
$reason = sanitize_text_field( $_POST['reason'] );
$message = sanitize_textarea_field( $_POST['message'] );
// Compose email
$to = get_option( 'admin_email' ); // send to WP admin email
$subject = "New Contact Form Submission from $name";
$body = "Name: $name\nEmail: $email\nPhone: $country $phone\nReason: $reason\nMessage:\n$message";
$headers = array(
'Content-Type: text/plain; charset=UTF-8',
'From: ' . $name . ' <' . $email . '>'
);
// Send email
$mail_sent = wp_mail( $to, $subject, $body, $headers );
if ( $mail_sent ) {
wp_send_json_success( array( 'message' => 'Thank you! Your message has been sent.' ) );
} else {
wp_send_json_error( array( 'message' => 'Sorry, your message could not be sent. Please try again.' ) );
}
wp_die(); // Required to end AJAX properly
}