2012-09-14 109 views
0

嗨,我已經創建了一個頁面與我們聯繫,但它沒有工作,爲什麼在那裏我錯了電子郵件不是在PHP發送爲什麼

請幫助我

接觸網頁代碼

<?php 
if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "Your email subject line"; 


    function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 

    // validation expected data exists 
    if(!isset($_POST['user_name']) || 
     !isset($_POST['contact_no']) || 
     !isset($_POST['contact_email_id']) || 
     !isset($_POST['city']) || 
     !isset($_POST['project']) || 
     !isset($_POST['local_property']) || 
     !isset($_POST['user_query'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $user_name = $_POST['user_name']; // required 
    $contact_no = $_POST['contact_no']; // required 
    $email_from = $_POST['contact_email_id']; // required 
    $city = $_POST['city']; // not required 
    $project = $_POST['project']; // not required 
    $local_property = $_POST['local_property']; // not required 
    $user_query = $_POST['user_query']; // required 

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
    if(!preg_match($email_exp,$email_from)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
    $string_exp = "/^[A-Za-z .'-]+$/"; 
    if(!preg_match($string_exp,$user_name)) { 
    $error_message .= 'The Name you entered does not appear to be valid.<br />'; 
    } 
    if(!preg_match($string_exp,$contact_no)) { 
    $error_message .= 'The Contact Number you entered does not appear to be valid.<br />'; 
    } 
    if(strlen($user_query) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
    } 
    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 
    $email_message = "Form details below.\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "User Name: ".clean_string($user_name)."\n"; 
    $email_message .= "Contact No: ".clean_string($contact_no)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "City: ".clean_string($city)."\n"; 
    $email_message .= "Project: ".clean_string($project)."\n"; 
    $email_message .= "Local Property: ".clean_string($local_property)."\n"; 
    $email_message .= "User Query: ".clean_string($user_query)."\n"; 


// create email headers 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- include your own success html here --> 

Thank you for contacting us. We will be in touch with you very soon. 

<?php 
} 
?> 

HTML代碼

<div class="contact_form"> 
    <p>Quick Contact</p> 
    <form action="contactus.php" method="post" name="contactform"> 
     <label><span>name:-</span> <input type="text" name="user_name"></label> 
     <label><span>contact no:-</span> <input type="text" name="contact_no"></label> 
     <label><span>email id:-</span> <input type="text" name="contact_email_id"></label> 
     <label><span>city:-</span> <input type="text" name="city"></label> 
     <label><span>projects:-</span> <input type="text" name="project"></label> 
     <label><span>local property:-</span> <input type="text" name="local_property"></label> 
     <label><span>query:-</span> <textarea row="" cols="" name="user_query"></textarea></label> 
     <input type="submit" value="submit"> 
    </form> 
</div> 
+0

您剛剛複製粘貼嗎?你遇到什麼錯誤? –

+0

您是否在錯誤日誌中收到任何消息? – Balanivash

+1

請詳細說明「它不工作」 – moopet

回答

0

將此內容添加到您的代碼中...

$headers = "From: \"$from_name\" <$from_mail>\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/plain; charset=\"utf-8\"\r\n"; 
$headers .= "Content-Transfer-Encoding: 7bit\r\n"; 
+0

在哪裏添加此代碼 –

+0

@Rohit Azad將其添加到頭聲明 – Ryhan

+0

@Rythan它不工作 –

相關問題