‘POST’,
‘callback’ => ‘sm_contact_handler’,
‘permission_callback’ => ‘__return_true’,
]);
});
function sm_contact_handler(WP_REST_Request $req) {
$name = sanitize_text_field($req->get_param(‘name’));
$email = sanitize_email($req->get_param(’email’));
$model = sanitize_text_field($req->get_param(‘model’) ?: ‘Not specified’);
$message = sanitize_textarea_field($req->get_param(‘message’));
if (!$name || !is_email($email) || !$message) {
return new WP_Error(‘invalid’, ‘Missing required fields’, [‘status’ => 400]);
}
$subject = ‘New Enquiry — Scottish McLarens Website’;
$body = “You have a new contact form submission:\n\nName: {$name}\nEmail: {$email}\nMcLaren Model: {$model}\n\nMessage:\n{$message}”;
$headers = [‘Content-Type: text/plain; charset=UTF-8’, “Reply-To: {$name} <{$email}>“];
$sent = wp_mail(‘hello@scottishmclarens.com’, $subject, $body, $headers);
return rest_ensure_response([
‘success’ => $sent,
‘message’ => $sent ? ‘Thanks! We will be in touch shortly.’ : ‘Sorry, there was an error sending your message.’,
]);
}