The following code is sending an email from my website, but the email comes from cgi-mailer@kundenserver.de
, 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'] ."
"
.'Email: ' .$_POST['Email'] ."
"
.'Message: ' .$_POST['Message'];
$email = $_GET['Email'];
mail('me@example.com', '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.
Declare the variable in the headers..
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "
" .
'Reply-To: webmaster@example.com' . "
" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Edit:
<?php
if(isset($_POST['submit'])) {
$msg = 'Name: ' .$_POST['FirstName'] .$_POST['LastName'] ."
"
.'Email: ' .$_POST['Email'] ."
"
.'Message: ' .$_POST['Message'];
$email = $_GET['Email'];
$headers = 'From: '.$email."
" .
'X-Mailer: PHP/' . phpversion();
mail('me@example.com', 'Message from website', $msg, $headers );
header('location: contact-thanks.php');
} else {
header('location: contact.php');
exit(0);
}
?>
Add this to the header
$headers .= 'From: ' . $from . "
";
$headers .='Reply-To: $from' . "
" ;
mail($to,$subject,$message,$headers);
It should set the sender.
where
$from= "Marie Debra <marie.debra@website.com>;"
<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
For more reference
$from = $_POST['email'];
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);