简体   繁体   中英

Allowing someone to reply to an email sent from a site with a php contact form

I have created a php contact form to allow someone to contact a company from a form on their website.

The code works fine and the email sends without a problem. The only problem is that when the email is sent and the company wish to replay to it, it sends to a strange email rather than to the email that the user entered within the contact form. Is there any way round this?

Here is the php code for the contact form.

 <?php if (isset($_POST["submit"])) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'site user'; $to = 'example@example.com'; $subject = 'Message from a site user'; $body ="From: $name\\n E-Mail: $email\\n Message:\\n $message"; if (!$_POST['name']) { $errorName = 'Please enter your name'; } if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $errorEmail = 'Please enter a valid email address'; } if (!$_POST['message']) { $errorMessage = 'Please enter your message'; } if (!$errorName && !$errorEmail && !$errorMessage) { if (mail ($to, $subject, $body, $from)) { $result='<div class="alert alert-success">Thank You! I will be in touch</div>'; } else { $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>'; } } } ?>

Add Reply To in your header,

Instead of passing $from as it is, do something like:

$from= 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM