2013-09-25 6 views
6

में उत्तर-पता पता मैंने एक PHP संपर्क फ़ॉर्म के साथ एक साधारण वेबसाइट टेम्पलेट खरीदा। वास्तव में फॉर्म के माध्यम से भेजे गए संदेशों को प्राप्त करने के एक छोटे अपवाद के साथ सबकुछ बढ़िया काम करता है। यही है, संपर्क फ़ॉर्म एक सफल संदेश दिखाएगा, लेकिन संदेश कभी नहीं आएगा।PHP संपर्क फ़ॉर्म

मेरी होस्टिंग सेवा के साथ लंबे समय से आगे के बाद, मुझे पता चला कि स्पूफिंग से बचने के लिए वे ईमेल भेजने की इजाजत नहीं देंगे, जहां से पता से वे होस्ट नहीं करते हैं। यही है, अगर साइट के विज़िटर फॉर्म में अपने जीमेल/याहू इत्यादि लिखते हैं, तो मुझे यह नहीं मिलेगा।

उन्होंने सुझाव के रूप में उनके साथ होस्ट किए गए ईमेल पते का उपयोग करने का सुझाव दिया, और आगंतुक के इनपुट ईमेल को उत्तर-पते के रूप में संबोधित किया। यह उचित लगता है।

तो मैं चारों ओर खोदा (यहाँ उदाहरण के लिए: 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); 

यह सब अच्छा और dandy है लेकिन यह अभी भी अच्छी तरह से काम नहीं करता। ईमेल अब के माध्यम से जाना है, लेकिन विवरण हैं:

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

जब आप इसे बनाते हैं तो आपको $ हेडर स्ट्रिंग के चारों ओर डबल कोट्स का उपयोग करने की आवश्यकता होती है - सिंगल कोट्स स्ट्रिंग को शाब्दिक के रूप में मानते हैं, इसलिए वेरिएबल इंटरपोलेट नहीं होते हैं। – 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(); 

मूल रूप से लें $ एकल कोट के अंदर से और उसे उस स्ट्रिंग में संलग्न करें

+0

दोनों धन्यवाद। यह काम करता प्रतीत होता है। मेरे पास साझा करने के लिए कर्म नहीं है ... क्षमा करें – MajorKooter

+0

@MajorKooter - यदि यह काम करता है, तो आप इस उत्तर को स्वीकृत के रूप में चिह्नित कर सकते हैं। – andrewsi

+1

@andrewsi धन्यवाद, हालांकि आप क्रेडिट के लायक हैं क्योंकि मैंने यही उपयोग किया है। आपके कमेंट के लिए धन्यवाद। – MajorKooter

संबंधित मुद्दे