我用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, $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;
}
?>
答案 0 :(得分:11)
尝试更改代码的这一部分:
$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 并将其附加到该字符串
答案 1 :(得分:0)
尝试使用这样的标题:
$headers = array(
'From' => $from,
'To' => $to,
'Cci' => $bcc,
'Subject' => $subject,
'Reply-To' => $reply_to
);