S. Kobe
S. Kobe

Reputation: 29

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>'; 
}
}
?>

Upvotes: 0

Views: 300

Answers (1)

Sinisa Bobic
Sinisa Bobic

Reputation: 1313

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."

Upvotes: 1

Related Questions