2013-09-25 13 views
6

PHPの連絡フォームを使って簡単なウェブサイトテンプレートを購入しました。フォームを介して送信されたメッセージを実際に受信するという小さな例外を除いて、すべてがうまくいきます。つまり、連絡先フォームには成功メッセージが表示されますが、メッセージは到着しません。phpの連絡フォームの返信先住所

私はホスティングサービスで長い時間を過ごした後、なりすましを避けるために、自分がホストしていないFROMアドレスのどこにメールを送信することができないことを知りました。つまり、サイトの訪問者が自分のgmail/yahooなどをフォームに記入すると、私はそれを取得しません。

彼らは、FROMアドレスとしてホストされている電子メールアドレスを使用し、訪問者の入力電子メールをREPLY-TOアドレスとして使用することを提案しました。これは妥当と思われます。

だから私の周り掘って(ここでは例: PHP reply-to error - comes with admin email not sender of contact formphp 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; 
} 

?> 
+3

作成時に$ headers文字列を二重引用符で囲む必要があります。一重引用符は文字列をリテラルとして扱うため、変数は補間されません。 – andrewsi

+0

非常にmcuh @andrewsiありがとう! :) – MajorKooter

答えて

9

はあなたのコードのこの部分を変更してみてください。これに

$subject = "Contact Form: $name"; 
$message = "$message"; 
$headers = 'From: [email protected]_domain.com' . "\r\n" . 
    'Reply-To: $email' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

$subject = "Contact Form: $name"; 
$message = "$message"; 
$headers = 'From: [email protected]_domain.com' . "\r\n" . 
    'Reply-To: ' . $email . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

基本的にはを取り出してください$ emailを一重引用符で囲み、その文字列に追加します。

+0

ありがとうございました。これはうまくいくようです。 私は共有するカルマを持っていません...申し訳ありません – MajorKooter

+0

@ MajorKooter - これがうまくいけば、この回答をAcceptedとマークできます。 – andrewsi

+1

@andrewsiありがとう、これは私が使ったものなので、あなたは信用を受けるべきです。ご意見ありがとうございます。 – MajorKooter

関連する問題