PHP 문의 양식을 사용하여 간단한 웹 사이트 템플릿을 구입했습니다. 모든 것은 양식을 통해 전송 된 메시지를 실제로받는 예외를 제외하고는 훌륭하게 작동합니다. 즉, 문의 양식에는 성공 메시지가 표시되지만 메시지는 절대로 도착하지 않습니다.PHP 문의 양식의 회신 주소
내 호스팅 서비스와 오랜 시간이 지난 후에 나는 스푸핑을 피하기 위해 발신자 주소가 아닌 발신 이메일을 허용하지 않는다는 것을 알았습니다. 즉, 사이트 방문자가 gmail/yahoo 등을 양식에 적어두면 얻을 수 없습니다.
그들은 그들과 함께 호스트 된 이메일 주소를 발신 주소로 사용하고 방문자의 입력 전자 메일을 회신 주소로 사용하도록 제안했습니다. 이것은 합리적인 것처럼 보입니다.
그래서 나는 주위 파고 (여기 예 : 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
감사합니다. 매우 mcuh @ andrewsi! :) – MajorKooter