So I've completed a contact form made in PHP. After testing it sends emails, but in the email header it is not capturing the sender's name or email address. It shows up as [email protected]
Is there a way to correct that? I need to be able to respond to emails.
The inputs are as follows:
<form id="contact" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="name" placeholder="Text input" value="<?= $name ?>" >
<p ><?= $name_error ?></p>
<input type="email" name="email" placeholder="Email Address" value="<?= $email?>">
<p ><?= $email_error?></p>
// back-end file
$sendTo = '[email protected]';
$subject = 'Business Enquiry';
$headers = "";
$headers .= "Sent From: ".$name. "\r\n";
$headers .= "Reply To: ".$email. "\r\n";
$headers .= "Message: ".$message. "\r\n";
if (mail($sendTo, $subject, $message, $headers)) {
$success = "Thank you for your message, I will reply as soon as I can.";
$name = $email = $message = ''; // resets all fields
}
CodePudding user response:
You cannot pick the names of the headers freely, you have to adhere to the standard (RFC 5322 Internet Message Format).
Your header "Sent From" should be just From
, your header "Reply To" should be Reply-To
and your header "Message" shouldn't be in the headers at all - you already pass the message as such into the mail function.
CodePudding user response:
All fixed now, thanks. All I needed was to specify one header ,
$headers = "From: ".$email;
So it's just $subject and $headers