0

I have a PHP contact form on my webpage but it does not send the email. I changed the code and looked into it but cannot find the reason. If anyone can help it will be appreciated.

<?php
 $name = $_POST['name'];
 $email = $_POST['email'];
 $subject = $_POST['subject'];
 $message = $_POST['message'];
 $error = "";
 $errorMessage = 'Sorry your message can not be sent.';

 //Validate first
 if(empty($name)||empty($email)||empty($message)) 
  {
echo "Name and email and message are required !";
header('Location: index.html');
 }

 //validate against any email injection attempts
 if(IsInjected($email))
  {
 echo "Bad email value!";
  header('Location: index.html');
   }


 $msg =  " Name : $name \r\n"; 
 $msg .= " Email: $email \r\n";
 $msg .= " Subject: $subject \r\n";
 $msg .= " Message : ".stripslashes($_POST['message'])."\r\n\n";
 $msg .= "User information \r\n"; 
 $msg .= "User IP : ".$_SERVER["REMOTE_ADDR"]."\r\n"; 
 $msg .= "Browser info : ".$_SERVER["HTTP_USER_AGENT"]."\r\n"; 
 $msg .= "User come from : ".$_SERVER["SERVER_NAME"];

 $recipient = "example@hotmail.com";// Change the recipient email adress to your adrees  
 $sujet =  "Sender information";
 $mailheaders = "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n";

 if (!$error){

    $sending = mail($recipient, $sujet, $msg, $mailheaders); 

    if ($sending) {
            // If the message is sent we output a string to use it 
            echo "SENDING"; 
        } else {
            // Display Error Message
            echo $errorMessage; 
        }
} else {
    echo $error; // Display Error Message
}


     // Function to validate against any email injection attempts
        function IsInjected($str)
        {
    $injections = array('(\n+)',
          '(\r+)',
          '(\t+)',
          '(%0A+)',
          '(%0D+)',
          '(%08+)',
          '(%09+)'
          );
     $inject = join('|', $injections);
     $inject = "/$inject/i";
     if(preg_match($inject,$str))
      {
      return true;
       }
     else
       {
    return false;
        }
        }

        ?>

I have edited the code and added comments so it will be easy to follow, I been working on it for days and just cannot figure out where I have made a mistake in the code.

awksp
  • 11,554
  • 4
  • 36
  • 44
bedri
  • 1
  • 1

0 Answers0