简体   繁体   中英

how to reply to sender email using php contact form

I am trying to adjust my code to able to reply to sender email from PHP contact form. please check my code below to give advise. Thank you

<?php

$marke = $_POST['marke'];
$modell = $_POST['modell'];
$name = $_POST['name'];
$adresse = $_POST['adresse'];
$telefon = $_POST['telefon'];
$email = $_POST['email'];

$to = 'myemail@gmail.com';
$from = 'myemail@gmail.com';
$subject = 'Contact Form';


$body = "marke: $marke\n modell: $modell\n name: $name\n adresse: $adresse\n 
email: $email\n telefon: $telefon\n";

?>

<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) { 
    header("Location: http://www.website.com/sent.php");
} else { 
    echo '<p>Oops! An error occurred. Try sending your message again.</p>'; 
}
}
?>

First make headers

$headers = "From: $from\r\nReply-to: $email";

Than fix calling of mail function to be

mail ($to, $subject, $body, $headers)

Didn't tried it from times when it was PHP 4 but it will probably work as you expected...

Addition: I just checked on php.net... go to this url http://php.net/manual/en/function.mail.php and check "Example #2 Sending mail with extra headers."

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