Emile Kelly
Emile Kelly

Reputation: 75

php contact form reply to sender

The following code is sending an email from my website, but the email comes from [email protected], how do i change this to the sender's email address, which i have given the variable $email:

<?php
if(isset($_POST['submit'])) {
    $msg = 'Name: ' .$_POST['FirstName'] .$_POST['LastName'] ."\n" 
    .'Email: ' .$_POST['Email'] ."\n" 
    .'Message: ' .$_POST['Message'];
$email = $_GET['Email'];
    mail('[email protected]', 'Message from website', $msg );
    header('location: contact-thanks.php');

    } else {
header('location: contact.php');
exit(0);
}
?>

Adding the header From: to my mail command seems to allow me to change the email address, but i can't work out how to do it to the variable.

Upvotes: 0

Views: 7083

Answers (4)

Geoff Turner
Geoff Turner

Reputation: 1

$from = $_POST['email'];

$headers = array('Content-Type: text/plain; charset="UTF-8";',
    'From: ' . $from,
    'Reply-To: ' . $from,
    'Return-Path: ' . $from,
);

Upvotes: 0

Bhuvan Rikka
Bhuvan Rikka

Reputation: 2703

Declare the variable in the headers..

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

Edit:

<?php
if(isset($_POST['submit'])) {
    $msg = 'Name: ' .$_POST['FirstName'] .$_POST['LastName'] ."\n" 
    .'Email: ' .$_POST['Email'] ."\n" 
    .'Message: ' .$_POST['Message'];
$email = $_GET['Email'];
$headers = 'From: '.$email."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    mail('[email protected]', 'Message from website', $msg, $headers );
    header('location: contact-thanks.php');

    } else {
header('location: contact.php');
exit(0);
}
?>

Upvotes: 2

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

<?php

$to = "[email protected]";

$subject = "Test mail";

$message = "Hello! This is a simple email message.";

$from = "[email protected]";

$headers = "From:" . $from;

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

echo "Mail Sent.";

?>

For more reference

http://php.net/manual/en/function.mail.php

Upvotes: 2

cjds
cjds

Reputation: 8426

Add this to the header

$headers .= 'From: ' . $from . "\r\n";
$headers .='Reply-To: $from' . "\r\n" ;
mail($to,$subject,$message,$headers);

It should set the sender.

where

$from= "Marie Debra <[email protected]>;"

Upvotes: 0

Related Questions