0

I was reading quite a few answers on how to build a php contact form here but without success since I am not sure what is wrong with my code. I am not sure if it is a server issue but my code is below and there is no email sent. Apologies but PHP is not my strong point. Can you let me know if there is a mistake in my code? If it doesn't work I was told it is better to use an SMTP

<?php
$name = $_POST['name'];

$visitor_email = $_POST['email'];
$telephone = $_POST['telephone'];
$request = $_POST['request'];

$book = $_POST['book'];

//Validate first
if(empty($name)||empty($visitor_email))
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'exampleemail@mail.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from  $name.\n".
    "Telephone:\n $telephone\n ".
    "Visitor Email: \n $visitor_email\n".
    "Book checked: \n $book\n".
        "Request checked: \n $request.\n";

$to = "example@mail.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: ../thank-you.html');


// 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;
  }
}

?>
theduck
  • 2,589
  • 13
  • 17
  • 23
Angelo D.
  • 5
  • 1
  • 7

1 Answers1

0

your problem is very common. It's because the mail is sent, but when you receive the mail, it is blocked by gmail/outlook because he thinks it can be a spam/bad email. You need to add better headers, check out this answer from another post : https://stackoverflow.com/a/18870250/12130402

Arlien
  • 132
  • 1
  • 17
  • Thanks for the reply, this is actually forwarded to an email account hosted on a server. We are checking the spam folders as well and nothing is there. Any more ideas? – Angelo D. Dec 12 '19 at 14:08