2

I can receive the message when a sender fills out the comments box of my form, and I can see the name of the sender. But, I cannot respond to the email because the email address is blank.

The php is as follows:

<?php

$name=$_POST['name'];
$telephone=$_POST['telephone'];
$email=$_POST['email'];
$date=$_POST['date'];
$location=$_POST['location'];
$comments=$_POST['comments'];
$to="info@hsphotography.co.uk";
$subject="new mesage";

mail($to,$subject,$comments,"From:".$name);
echo "Your message has been sent. Thank you for your enquiry";

?>
Hannele
  • 9,301
  • 6
  • 48
  • 68
  • Note the warning from the [php docs](http://php.net/manual/en/function.mail.php) "additional_headers does not have mail header injection protection. Therefore, users must make sure specified headers are safe and contains headers only. i.e. Never start mail body by putting multiple newlines." See this [question/answer](http://stackoverflow.com/questions/11952473/proper-prevention-of-mail-injection-in-php?answertab=votes#tab-top) for more info on this – Theo Feb 27 '17 at 23:24

2 Answers2

1

Fourth argument of mail() function accepted additional headers. For example:

$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";

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

You need Reply-To header.

1

Right now you're putting the $name variable in the field where you should be putting the sender's email address ($email). You can use both, but unfortunately you need to hack them together, since the PHP mail function doesn't do it for you.

You can add both like this:

mail($to, $subject, $comments, 'From: "' . $name . '" <' . $email . '>')

Note, you need to include double-quote characters inside the string, which can get a little dicey but you can use ' to avoid needing to get more complicated.

If $name is "Bob" and $email is "bob@test.test", then all together this should look like: 'From: "Bob" <bob@test.test>'. See also, this question on how to format email headers

Community
  • 1
  • 1
Hannele
  • 9,301
  • 6
  • 48
  • 68