php联系表单回复发件人

时间:2012-12-19 10:04:14

标签: php email sendmail

以下代码是从我的网站发送电子邮件,但该电子邮件来自cgi-mailer@kundenserver.de,如何将其更改为发件人的电子邮件地址,我已将变量$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);
}
?>

将标题From:添加到我的邮件命令似乎允许我更改电子邮件地址,但我无法弄清楚如何对变量执行此操作。

4 个答案:

答案 0 :(得分:2)

在标题中声明变量..

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

修改

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

答案 1 :(得分:2)

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

?>

更多参考资料

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

答案 2 :(得分:0)

将其添加到标题

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

应该设置发件人。

其中

$from= "Marie Debra <marie.debra@website.com>;"

答案 3 :(得分:0)

$from = $_POST['email'];

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