用php联系表格回复地址

Maj*_*ter 7 php forms email contact-form

我买了一个简单的网站模板与php联系表格.实际接收通过表单发送的消息的一个小例外,一切都很好.也就是说,联系表单会显示成功消息,但消息永远不会到达.

经过我的托管服务的漫长反复,我发现为了避免欺骗,他们不允许在他们不主持的FROM地址发送电子邮件.也就是说,如果该网站的访问者在表单中写下他的gmail/yahoo等,我将无法得到它.

他们建议使用托管在其中的电子邮件地址作为FROM地址,并将访问者的输入电子邮件作为REPLY-TO地址.这看似合理.

所以我挖了一下(例如这里: PHP回复错误 - 附带管理员电子邮件而非联系表格的发件人网站上的php联系表格以及回复电子邮件)

并且答案提示添加标题组件:

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

并将其添加到

mail($to, $subject, $message, $headers);
Run Code Online (Sandbox Code Playgroud)

这就是我做的.$ 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);
Run Code Online (Sandbox Code Playgroud)

这一切都很好,花花公子,但它仍然不能很好地工作.电子邮件现在通过,但细节是:

from:    myemail@my_domain.com via servername.hosting_company.com 
**reply-to:  $email@servername.hosting_company.com**
to:  myemail@my_domain.com
Run Code Online (Sandbox Code Playgroud)

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

你能帮帮我吗?不知道我还能做些什么.

非常感谢!


如果有人有兴趣,这里是完整的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;
}

?>
Run Code Online (Sandbox Code Playgroud)

Bri*_*yua 12

尝试更改代码的这一部分:

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

对此:

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

基本上从单引号中取出$ email并将其附加到该字符串

  • @MajorKooter这个答案仍然容易受到邮件头注入的影响; 在这种情况下,经常检查是否`$ _POST ["电子邮件"]`/`$ email`包含一个有效的电子邮件地址,否则垃圾邮件发送者将竭诚与您的形式通过注射接收相同的邮件多个电子邮件地址. (6认同)