php contact form reply to sender

北城余情 提交于 2019-12-20 06:14:24

问题


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'] ."\n" 
    .'Email: ' .$_POST['Email'] ."\n" 
    .'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.


回答1:


<?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

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




回答2:


Declare the variable in the headers..

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\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('me@example.com', 'Message from website', $msg, $headers );
    header('location: contact-thanks.php');

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



回答3:


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 <marie.debra@website.com>;"



回答4:


$from = $_POST['email'];

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


来源:https://stackoverflow.com/questions/13949834/php-contact-form-reply-to-sender

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!