2012-11-21 96 views
0

我在網站上使用聯繫表格,它不發送電子郵件,請任何人都可以告訴我這個代碼是錯誤的?
此表單是從另一個工作的網站複製的,所以我無法理解它失敗的位置。我檢查過語法,一切似乎都沒問題。簡單的PHP聯繫表格:提交按鈕不發送電子郵件

<?php 
/* 
Template Name: Contact Page 
*/ 

get_header(); 


$contact_show = true; 

//If the form is submitted 
if(isset($_POST['submit'])) { 

//Check to make sure that the name field is not empty 
if(trim($_POST['contactname']) == '') { 
    $hasError = true; 
} else { 
    $name = trim($_POST['contactname']); 
} 
//Check to make sure that the subject field is not empty 
if(trim($_POST['subject']) == '') { 
    $hasError = true; 
} else { 
    $subject = trim($_POST['subject']); 
} 

//Check to make sure sure that a valid email address is submitted 
if(trim($_POST['email']) == '') { 
    $hasError = true; 
} else if (!preg_match("/^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$/i",  trim($_POST['email']))) { 
    $hasError = true; 
} else { 
    $email = trim($_POST['email']); 
} 

//Check to make sure comments were entered 
if(trim($_POST['message']) == '') { 
    $hasError = true; 
} else { 
    if(function_exists('stripslashes')) { 
     $comments = stripslashes(trim($_POST['message'])); 
    } else { 
     $comments = trim($_POST['message']); 
    } 
} 

//If there is no error, send the email 
if(!isset($hasError)) { 
    $contact_show = false; 
    $emailTo = get_option('of_email_address'); 
    $body = "Name: $name \r\nEmail: $email \r\nSubject: $subject \r\nComments:\r\n $comments";   
    $headers = 'From: <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

    mail($emailTo, $subject, $body, $headers); 
    $emailSent = true; 
} 
} 
?> 
<div id="top"> 
<h1><?php the_title(); ?></h1> 
</div> 
</div> 
<div id="main_content"> 
<div id="contact"> 
<?php while (have_posts()) : the_post(); ?> 
<?php endwhile; // end of the loop. ?> 
<?php if ($contact_show == true) { ?> 
<?php } ?> 


     <?php if(isset($hasError)) { //If errors are found ?> 
      <div class="redbox"> 
       <p><?php _e('Please check if you\'ve filled all the fields with valid information. Thank you.') ?></p> 
      </div> 
     <?php } ?> 

     <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?> 
      <div class="greenbox"> 
       <p> 
        <?php _e('Your message has been sent!') ?><br /> 
        <?php echo $name; _e(' thank you for emailing us! We will get in touch with you soon.'); ?> 
       </p> 
      </div> 
     <?php } ?> 
     <form action="" method="post" id="contactForm"><!-- start contact form --> 
      <fieldset class="contact-fieldset"> 
       <ul> 
        <li> 
         <label for="contactname"><?php _e('Name:') ?></label> 
         <input type="text" name="contactname" id="name" class="contact-input requiredContact" /> 
        </li> 
        <li> 
         <label for="email"><?php _e('Email:') ?></label> 
         <input type="text" id="email" name="email" class="contact-input  requiredContact cEmail" /> 
        </li>  
        <li> 
         <label for="subject"><?php _e('Subject:') ?></label> 
         <input type="text" name="subject" id="subject" class="contact-input requiredContact" /> 
        </li> 
        <li> 
         <label for="message"><?php _e('Message:') ?></label> 
         <textarea rows="6" style="overflow: hidden;" cols="40" id="message" name="message" class="contact-textarea 

requiredContact"></textarea> 
        </li> 
        <li> 
         <input type="submit" value="<?php _e('send message'); ?>"  name="submit" class="contact-submit" 

/> 
        </li>      
       </ul> 
      </fieldset> 
     </form></div> 



</div> 
+0

是否有錯誤? 'mail()'返回什麼?如果它在別處工作,那麼它可能是服務器郵件配置。 – MrCode

回答

0

如果從其他網站,沒有工作coppied它,那麼就表明該PHP代碼的作品,它是其實你的服務器設置是錯誤的,因爲那是你已經改變了(而不是PHP代碼,如果它以前工作,我假設顯示沒有錯誤)。確保這些設置正確:

http://php.net/manual/en/mail.configuration.php

0

我檢查你的代碼,因爲沒有任何錯誤和不工作的原因是函數調用的可能。

在您的編碼中存在幾個函數調用。檢查所有功能是否存在,以及該功能是否存在錯誤。

0

再一次,PHP看起來不錯;檢查你的php.ini文件,並確保郵件功能被配置爲爲PHP提供一個sendmail或SMTP連接的路徑;另外,請確保您的防火牆設置不阻止端口25(或您的配置的SMTP中繼端口)

相關問題