title: Send an email to admin when user login in PHP
body: <p>I have a problem in my script but I am not sure why it happens, may be due to sessions or HTTP headers. I want my login script in this way "When user logs in, an email should be sent to admin with user's information", so by excluding email section in following code, simply login works well, also I tested email section in separate file that works fine. But I don't why email is not sending  and page appears as white blank screen with following code.</p>
  
  <pre><code>session_start();
  $errors = array();
  
  if(isset($_POST['submit']) == "Login") {
  
      if(empty($_POST['username'])) { $errors[] = "Please enter Username! ";} 
      else { $username = mysql_real_escape_string($_POST['username']); }
  
      if(empty($_POST['password'])) { $errors[] = "Please enter Password! ";} 
      else { $password = mysql_real_escape_string($_POST['password']);}   
  
      if(empty($errors)) {
          $sql = mysql_query("SELECT * FROM users WHERE `username` = '".$username."' AND `password` = sha1('".$password."')");
           $row = mysql_fetch_assoc($sql);
  
          if($row) {  
              $_SESSION['user_login'] = true;
              $_SESSION['user_id'] = $row['user_id'];
              $_SESSION['full_name'] = $row['full_name'];
  
              /*******************Send Email***************************/
              //Send user information to admin            
              $to = "xxx@XXX.com";
              $from = "XXX@xxx.com";
  
              //Get User Information
              $Full_Name = $row['full_name'];
              $Browser = $_SERVER['HTTP_USER_AGENT'];          
              $User_IP = $_SERVER['REMOTE_ADDR'];
              // If IP address exists
              // Get country (and City) via  api.hostip.info
              if (!empty($User_IP)) {
                  $country=file_get_contents('http://api.hostip.info/get_html.php?ip='.$User_IP);
                  list ($_country) = explode ("\n", $country);
                  $_country = str_replace("Country: ", "", $_country);
              }                        
  
              $subject = $Full_Name. " logged in!";
  
              $message = '&lt;html&gt;&lt;body&gt;';          
              $message .= '&lt;table rules = "all" border="0" cellpadding="10"&gt;';
              $message .= "&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;";
              $message .= "&lt;tr style='background: #eee;'&gt;&lt;td colspan='2'&gt;&lt;strong&gt;Dear Admin!&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;";
              $message .= "&lt;tr&gt;&lt;td colspan='2'&gt;A User &lt;strong&gt;" .$Full_Name. "&lt;/strong&gt; logged in with following information! &lt;/td&gt;&lt;/tr&gt;";                
              $message .= "&lt;tr&gt;&lt;td width='30%'&gt;&lt;strong&gt;Name ID:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;" . $Full_Name  . "&lt;/td&gt;&lt;/tr&gt;";
              $message .= "&lt;tr&gt;&lt;td&gt;&lt;strong&gt;IP Address:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;" . $User_IP  . "&lt;/td&gt;&lt;/tr&gt;";
              $message .= "&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Country:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;" . $_country  . "&lt;/td&gt;&lt;/tr&gt;";
              $message .= "&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Browser:&lt;/strong&gt; &lt;/td&gt;&lt;td&gt;" . $Browser  . "&lt;/td&gt;&lt;/tr&gt;";                       
              $message .= "&lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;";
              $message .= "&lt;/table&gt;";             
              $message .= "&lt;/body&gt;&lt;/html&gt;";   
              $headers = "From: " . $from . "\r\n";
              $headers .= "MIME-Version: 1.0\r\n";
              $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
              mail($to, $subject, $message, $headers) or die("Error!");
  
              echo "&lt;script&gt;window.open('index.php','_self')&lt;/script&gt;";
  
           } else {
              $errors[] = "Incorrect login, please try again!";
           }  
      }#Empty Errors
  
  }#End Submit
  </code></pre>