PHP联系表单中的回复地址

11

我购买了一个包含 php 联系表单的简单网站模板。除了收不到通过表单发送的信息之外,一切都运行得很好。也就是说,联系表单会显示成功发送信息的消息,但实际上我却没有收到任何信息。

在与我的主机服务长时间地来回沟通后,我发现为了避免欺骗行为,他们不允许发送 FROM 地址非本主机的邮件。也就是说,如果站点访问者在表单中填写了他的 Gmail/Yahoo 等电子邮件地址,那么我将无法收到这些信息。

他们建议使用由他们提供的电子邮件地址作为 FROM 地址,并将访问者提供的电子邮件地址作为 REPLY-TO 地址。这个方案听起来很合理。

于是我找到了一些相关问题(例如:PHP reply-to error - comes with admin email not sender of contact formphp Contact Form on website and reply-to email),答案中提到添加一个 headers 组件:

$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

并将其添加到

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

这就是我所做的事情。 在此模板中,$email被定义为访问者的电子邮件地址,因此我所做的是:

$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: myemail@my_domain.com' . "\r\n" .
    'Reply-To: $email' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

这一切看起来都很好,但实际上它仍然无法很好地工作。 电子邮件现在确实可以通过,但是具体情况如下:

from:    myemail@my_domain.com via servername.hosting_company.com 
**reply-to:  $email@servername.hosting_company.com**
to:  myemail@my_domain.com

所以,回复的地址仍然不是访客留下的。

你能帮我解决这个问题吗?不知道我还能做什么。

非常感谢!


如果有人感兴趣,这是完整的PHP文件:

<?php

// Clean up the input values
foreach($_POST as $key => $value) {
    if(ini_get('magic_quotes_gpc'))
        $_POST[$key] = stripslashes($_POST[$key]);

    $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}

// Assign the input values to variables for easy reference
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

// Test input values for errors
$errors = array();
if(strlen($name) < 2) {
    if(!$name) {
        $errors[] = "You must enter a name.";
    } else {
        $errors[] = "Name must be at least 2 characters.";
    }
}
if(!$email) {
    $errors[] = "You must enter an email.";
} else if(!validEmail($email)) {
    $errors[] = "You must enter a valid email.";
}
if(strlen($message) < 10) {
    if(!$message) {
        $errors[] = "You must enter a message.";
    } else {
        $errors[] = "Message must be at least 10 characters.";
    }
}

if($errors) {
    // Output errors and die with a failure message
    $errortext = "";
    foreach($errors as $error) {
        $errortext .= "<li>".$error."</li>";
    }
    die("<span class='failure'><h3>Sorry, The following errors occured:</h3><ol>". $errortext ."</ol><a href='contact.html' class='more'>Refresh Form</a></span>");
}


// --------------------------------------//
// Send the email // INSERT YOUR EMAIL HERE
$to = "myemail@my_domain.com";
// --------------------------------------//


$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: myemail@my_domain.com' . "\r\n" .
    'Reply-To: $email' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


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

// Die with a success message
die("<span class='success'><h3>Successfully Sent!</h3> Your message is on its way, we will respond to you shortly.</span>");

// A function that checks to see if
// an email is valid
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

?>

5
在创建 $headers 字符串时,你需要使用双引号括起来 - 单引号会将字符串视为字面量,因此变量不会被内插。 - andrewsi
2个回答

16

试着修改你代码的这一部分:

$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: myemail@my_domain.com' . "\r\n" .
    'Reply-To: $email' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

转换为:

$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: myemail@my_domain.com' . "\r\n" .
    'Reply-To: ' . $email . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

从单引号内提取$email并将其附加到该字符串中。


谢谢你们两个。这似乎有效。 我没有足够的声望来分享...抱歉。 - MajorKooter
1
@andrewsi 谢谢,虽然这是我使用的内容,但你应该得到荣誉。感谢你的评论。 - MajorKooter
7
这个答案仍然容易受到电子邮件标题注入的攻击。在这种情况下,应始终检查$_POST ["email"]/ $email是否包含有效的电子邮件地址,否则垃圾邮件发送者将通过注入多个电子邮件地址来利用您的表单。请注意避免此类情况。 - Marcel Korpel

1
尝试像这样使用标题:

$headers = array(
    'From' => $from,
    'To' => $to,
    'Cci' => $bcc,
    'Subject' => $subject,
    'Reply-To' => $reply_to
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接