Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

html - PHP contact form - Message sent blank

Good morning,

I am trying to set up a PHP contact form. The email is sent properly but there is no content at all (no subject, no message body and when i try to reply to the message the recipient address is not taken from the email input in the contact form, but to the answer will be redirected to : [email protected]).

I am using a mail handler ([email protected]) that belongs to my domain to avoid the fact that some servers could consider the email as a SPOOFING email and therefore there is the possibility that the email could not be delivered. In the HEADERS attribute i specified the email that the user enters it in the contact form as the "Reply-To" email. But in this case when i try to reply to the "blank" email, the "Reply-To" is strangely the mail handler ([email protected]).

Here is the PHP code :

<?php

$contactname = "";
$companyname = "";
$phone = "";
$email = "";
$subject = "";
$message = "";
$postmaster ="[email protected]";

if(isset($_POST['contactname'], $_POST['companyname'], $_POST['phone'], $_POST['email'], $_POST['subject'], $_POST['message'])){

  $contactname = htmlspecialchars($_POST['contactname']);
  $companyname = htmlspecialchars($_POST['companyname']);
  $phone = htmlspecialchars($_POST['phone']);
  $email = htmlspecialchars($_POST['email']);
  $subject = htmlspecialchars($_POST['subject']);
  $message = htmlspecialchars($_POST['message']);
}

$email_body = "You have received a new message from ".$companyname.".

 Phone Number is: ".$phone."

 Here is the message: ".$message;
    
$to = "[email protected]";
$headers = "From: $postmaster 
";
$headers .= "Reply-To: $email 
";
$headers .= "Content-Type: text/html; charset=iso-8859-1
";

mail($to,$subject,$email_body,$headers);

header('Location: success.html');

function IsInjected($str)
{
  $injections = array('(
+)',
              '(
+)',
              '(+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
   
?> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code is not good. Seems like you are a beginner. I have drafted a better code for you. Try this!

$contactName = (!empty($_POST['contactname']))?$_POST['contactname']:null;
$companyName = (!empty($_POST['companyname']))?$_POST['companyname']:null;
$phone = (!empty($_POST['phone']))?$_POST['phone']:null;
$email = (!empty($_POST['email']))?$_POST['email']:null;
$subject = (!empty($_POST['subject']))?$_POST['subject']:null;
$message = (!empty($_POST['message']))?$_POST['message']:null;

$postMaster ="[email protected]";
$to = "[email protected]";

if($_POST){
  $body = "Hello! You have received a new mail from ".$companyName."<br><br><b>Phone:</b> ".$phone.".<br><br><b>Message:</b> ".$message;
  $headers = "From: $postmaster 
";
  $headers .= "Reply-To: $email 
";
  $headers .= "Content-Type: text/html; charset=iso-8859-1
";

  if(mail($to,$subject,$body,$headers)){
    header('Location: success.html');
    exit();
  }else{
    header('Location: failed.html');
    exit();
  }
}

You must learn to write cleaner, shorter and well formatted codes. Also, I did not understand what your injection function does. Hence, I left that out. To me it seemed unnecessary. However, if you anyhow feel it was necessary for you, please feel free to include it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...