You can use the $_SERVER['HTTP_HOST'] variable to detect if the user is on localhost in PHP.
Here is a short tutorial that will assist you in configuring XAMPP to send email from localhost using PHP.