【问题标题】:php contact form reply to senderphp联系表格回复发件人
【发布时间】:2024-04-22 08:10:01
【问题描述】:

以下代码是从我的网站发送一封电子邮件,但电子邮件来自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: 添加到我的邮件命令似乎允许我更改电子邮件地址,但我不知道如何对变量执行此操作。

【问题讨论】:

  • 您可能需要在标题中提供Reply-To:。你看过mail()函数的例子吗?
  • 我已经尝试过了,但它只适用于硬编码的电子邮件地址,而不是变量。与发件人相同:

标签: php email sendmail


【解决方案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.";

?>

更多参考

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

【讨论】:

  • 但这只会发送回一个硬编码的电子邮件地址?我需要将它发送回一个电子邮件地址,该地址会因每个使用我的表单的人而改变..
  • from 应该是这样的: $from = "Someone Else \r\n";不应该吗?最佳实践。
  • 但这是一个硬编码的电子邮件地址。当我回复由此生成的电子邮件时,它不会发送给填写表格的人。
  • 什么意思?看看我的代码: $email = $_GET['Email']; - 所以我的变量 $email 应该可以不使用了吗?
【解决方案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);
}
?>

【讨论】:

  • 所以我的看起来像: &headers = 'From: $email' 。 “\r\n”。 ?
  • 不。只是来自 $email,而不是我在表单中输入的实际电子邮件地址。
  • 仍然没有喜悦。现在 from 或 reply to 字段中没有任何内容。
【解决方案3】:

将其添加到标题

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

它应该设置发件人。

在哪里

$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,
    );
    

    【讨论】: