2013-02-23 292 views
0

我已經爲我的網站的聯繫頁面創建了以下php腳本。腳本的問題是,當用戶填寫並單擊提交按鈕時,它會顯示成功消息,但不發送電子郵件。
有人可以檢查並告訴我我哪裏出錯了。聯繫表顯示成功消息,但不發送電子郵件

<?php 
error_reporting(E_ALL^E_NOTICE); // hide all basic notices from PHP 

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

// require a name from user 
if(trim($_POST['contactName']) === '') { 
    $nameError = 'Forgot your name!'; 
    $hasError = true; 
} else { 
    $name = trim($_POST['contactName']); 
} 

// need valid email 
if(trim($_POST['email']) === '') { 
    $emailError = 'Forgot to enter in your e-mail address.'; 
    $hasError = true; 
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) { 
    $emailError = 'You entered an invalid email address.'; 
    $hasError = true; 
} else { 
    $email = trim($_POST['email']); 
} 

// we need at least some content 
if(trim($_POST['comments']) === '') { 
    $commentError = 'You forgot to enter a message!'; 
    $hasError = true; 
} else { 
    if(function_exists('stripslashes')) { 
     $comments = stripslashes(trim($_POST['comments'])); 
    } else { 
     $comments = trim($_POST['comments']); 
    } 
} 

// upon no failure errors let's email now! 
if(!isset($hasError)) { 

    $emailTo = '[email protected]'; 
    $subject = 'Submitted message from '.$name; 
    $sendCopy = trim($_POST['sendCopy']); 
    $body = "Name: $name \n\nEmail: $email \n\nComments: $comments"; 
    $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

    mail($emailTo, $subject, $body, $headers); 

    // set our boolean completion value to TRUE 
    $emailSent = true; 
} 
} 
?> 

<!-- @begin contact --> 
<div id="contact" class="section"> 
    <div class="container content"> 

     <?php if(isset($emailSent) && $emailSent == true) { ?> 
      <p class="info">Your email was sent. Sir!</p> 
     <?php } else { ?> 



      <div id="contact-form"> 
       <?php if(isset($hasError) || isset($captchaError)) { ?> 
        <p class="alert">Error submitting the form</p> 
       <?php } ?> 

       <form id="contact-us" action="contacts.php" method="post"> 
      <div class="columns two contact_label alpha"> 
       Your Name 
      </div> 
      <div class="columns five contact_input omega"> 
       <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="txt requiredField" placeholder="Name:" /> 
         <?php if($nameError != '') { ?> 
          <br /><span class="error"><?php echo $nameError;?></span> 
         <?php } ?> 
        </div> 

        <div class="clear"><!-- ClearFix --></div> 
      <div class="columns two contact_label alpha"> 
       E-Mail Address 
      </div> 
      <div class="columns five contact_input omega"> 
       <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="txt requiredField email" placeholder="Email:" /> 
         <?php if($emailError != '') { ?> 
          <br /><span class="error"><?php echo $emailError;?></span> 
         <?php } ?> 
        </div> 
      <div class="clear"><!-- ClearFix --></div> 
       <div class="columns two contact_label alpha"> 
       Your Message 
      </div> 
      <div class="columns five contact_input omega"> 
       <textarea name="comments" id="commentsText" cols="45" rows="5" class="txtarea requiredField" placeholder="Message:"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea> 
         <?php if($commentError != '') { ?> 
          <br /><span class="error"><?php echo $commentError;?></span> 
         <?php } ?> 
        </div> 
      <div class="clear"><!-- ClearFix --></div> 

       <div class="columns five contact_button alpha omega offset-by-two"> 
       <input type="submit" name="submitted" id="submitted" value="Send Email" /> 
      </div> 
      <div class="clear"><!-- ClearFix --></div> 


       </form>   

      </div> 
     <?php } ?> 
    </div> 
</div><!-- End #contact --> 
+0

你確定你的服務器配置爲發送郵件嗎?這對夫婦迄今爲止得到的答案告訴你檢查從'mail()'返回的實際狀態,而不是假定它已經發送,如果你做了這個改變,它仍然失敗,這可能是因爲服務器贏了不允許你發送郵件。 – 2013-02-23 21:26:31

+0

你有什麼建議我應該在哪裏檢查我的服務器? – 2013-02-23 21:48:21

+0

我會從[php.ini中的sendmail東西]開始(http://www.php.net/manual/en/mail.configuration.php),特別是'sendmail_path',確保它指向一個有效的sendmail安裝。 – 2013-02-23 22:54:25

回答

2

您需要刪除行:

$emailSent = true; 

並更改郵件發送行

$emailSent = mail($emailTo, $subject, $body, $headers);

它設置$ emailSent爲true,如果郵件被接受交付。 PHP Mail

+0

謝謝大家。該代碼仍然無法正常工作,我的網絡主機告訴我他們的服務器正常。還有其他建議嗎? – 2013-02-23 21:46:46

1

而不是

mail($emailTo, $subject, $body, $headers); 

    // set our boolean completion value to TRUE 
    $emailSent = true; 

$emailSent = mail($emailTo, $subject, $body, $headers);

否則你會被硬編碼的結果,爲什麼你會說,如果你不知道該郵件已發送?你是什​​麼人,某種騙子?

0

我想,你應該嘗試改變如下:

if(!isset($hasError)) { 
    ... 
} 

條件。您應該輸入:

if(!$hasError) 
{ 
    ... 
} 
0

確保您的php郵件smtp標頭已正確配置。下面的 是適用於我的示例代碼。複製並嘗試它。

如果以下代碼不適用於您,請嘗試安裝Dropifi Contact Widget (www.dropifi.com)。他們有一個非常直觀的聯繫表格,您可以自定義以滿足您的需求。

require_once("class.phpmailer.php"); 
require_once("class.smtp.php"); 

    class Email extends PHPMailer 
    {  
     public function __construct(){ 
      parent::__construct(true); 
      $this->IsSMTP(); 
      $this->SMTPAuth = true; 
      $this->SMTPSecure = "ssl"; 
      $this->Host = "smtp.gmail.com"; 
      $this->Port = 465; 
      $this->Username = "[email protected]"; 
      $this->Password = "your password"; 
      $this->from = "[email protected]"; 
      $this->FromName = "Webmaster"; 
      $this->WordWrap = 50; 
      $this->IsHTML(true); 
      $this->AddReplyTo("[email protected]", "Webmaster"); 
      $this->Mailer = "smtp"; 
     } 

     public function setReceiver($to){ 
      $this->AddAddress($to,"Administrator"); 
     } 

     public function setSubject($subject){ 
      $this->Subject = $subject; 
     } 

     public function setMessage($param){ 

      $this->AltBody = "your alternative message"; 

      $message = "your main message"; 
      $this->MsgHTML($message); 
     } 

     public function sendmail(){ 
      try{ 
       $flag = $this->Send(); 
       return $flag; 
      }catch(phpmailerException $e){ 
       return $e->getMessage(); 
      } 
     } 
    }        
相關問題