我用php聯繫表格購買了一個簡單的網站模板。除了實際接收通過表單發送的消息之外,一切都很好。也就是說,聯繫表單會顯示成功消息,但消息永遠不會到達。回覆地址在PHP聯繫表格
經過與我的託管服務很長時間後,我發現爲了避免欺騙,他們不會允許將電子郵件發送到他們沒有託管的FROM地址處。也就是說,如果該網站的訪問者在表單中記下他的Gmail /雅虎等,我不會得到它。
他們建議使用與他們一起託管的電子郵件地址作爲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: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
,並把它添加到
mail($to, $subject, $message, $headers);
所以這就是我所做的。 $電子郵件在這個模板什麼的訪問者的郵件定義,所以我所做的就是:
$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: [email protected]_domain.com' . "\r\n" .
'Reply-To: $email' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
這是所有好的和花花公子,但它仍然不能很好地工作。 電子郵件做經歷了,但細節:
from: [email protected]_domain.com via servername.hosting_company.com
**reply-to: [email protected]_company.com**
to: [email protected]_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 = "[email protected]_domain.com";
// --------------------------------------//
$subject = "Contact Form: $name";
$message = "$message";
$headers = 'From: [email protected]_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;
}
?>
當您創建它時,您需要在$ headers字符串周圍使用雙引號 - 單引號將字符串視爲文字,因此變量不會插值。 – andrewsi
非常感謝@andrewsi! :) – MajorKooter