我买了一个简单的网站模板与PHP联系表格.实际接收通过表单发送的消息的一个小例外,一切都很好.也就是说,联系表单会显示成功消息,但消息永远不会到达.
经过我的托管服务很长时间来回,我发现,为了避免欺骗,他们不允许在他们不主持的FROM地址发送电子邮件.也就是说,如果该网站的访问者在表单中写下他的gmail / yahoo等,我将无法得到它.
他们建议使用托管在其中的电子邮件地址作为FROM地址,并将访问者的输入电子邮件作为REPLY-TO地址.这看似合理.
所以我挖了一下(例如这里:
PHP reply-to error – comes with admin email not sender of contact form
和
php Contact Form on website and reply-to email)
$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,$headers);
这一切都很好,花花公子,但它仍然不能很好地工作.
电子邮件现在可以通过,但细节是:
from: myemail@my_domain.com via servername.hosting_company.com **reply-to: $email@servername.hosting_company.com** to: myemail@my_domain.com
所以,回复地址仍然不是访客留下的.
你能帮我解决这个问题吗?不知道我还能做些什么.
非常感谢!
<?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,$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,$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('/^"(\\\\"|[^"])+"$/',$local))) { $isValid = false; } } if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { // domain not found in DNS $isValid = false; } } return $isValid; } ?>
尝试更改代码的这一部分:
$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并将其附加到该字符串