2011-04-01 75 views
0

我有一個表格,除了將輸入值發送到我的電子郵件之外,一切正常,我做錯了什麼? Ps:不使用本地服務器,所以不是這樣。HTML/PHP表格不發送電子郵件

編輯:我沒有收到任何電子郵件。

  • 嘗試更改if(isset($ _ POST ['enviar'])){part但仍然不工作。

  • 嘗試了健談的回聲。唯一的表現不正常的行爲是反扒。它停在else語句處。

的表單代碼:

<div id="contact-wrapper"> 

    <?php if(isset($hasError)) { //If errors are found ?> 
     <p class="error">Please check if you entered valid information.</p> 
    <?php } ?> 

    <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?> 
     <p><strong>Email sent with success!</strong></p> 
     <p>Thank you for using our contact form <strong><?php echo $name;?></strong>, we will contact you soon.</p> 
    <?php } ?> 

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform"> 
     <div> 
      <label for="name"><strong>Name:</strong></label> 
      <input type="text" size="50" name="contactname" id="contactname" value="" class="required" /> 
     </div> 

     <div> 
      <label for="email"><strong>E-mail:</strong></label> 
      <input type="text" size="50" name="email" id="email" value="" class="required email" /> 
     </div> 

     <div> 
      <label for="subject"><strong>Subject:</strong></label> 
      <input type="text" size="50" name="subject" id="subject" value="" class="required" /> 
     </div> 

     <div> 
      <label for="message"><strong>Message:</strong></label> 
      <textarea rows="5" cols="50" name="message" id="message" class="required"></textarea> 
     </div> 
     <input type="submit" value="enviar" name="submit" id="submit" /> 
    </form> 
    </div> 

和PHP:

<?php 
//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 (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", 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)) { 
     $emailTo = '[email protected]'; //Put your own email address here 
     $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments"; 
     $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

     mail($emailTo, $subject, $body, $headers); 
     $emailSent = true; 
    } 
} 
?> 
+1

您確定腳本正在執行mail()函數調用嗎? – 2011-04-01 21:55:26

回答

1

ereg()家族的功能已被棄用。改爲使用preg_...()等價物。他們的工作幾乎完全相同,除了需要圍繞匹配模式的分隔符。

同樣,不要在您的表單中使用PHP_SELF。該值是用戶提供的原始數據,可以輕微地針對XSS攻擊進行破壞。

檢查特定的表單字段以查看發生的POST是否有些不可靠 - 稍後您可能會更改該字段的名稱,並且檢查將失敗。然而,這

if ($_SERVER['REQUEST_METHOD'] == 'POST) { ... } 

永遠是可行的,不管多還是少的字段如何在形式,只要形式實際上公佈。

至於實際的問題,我假設郵件被髮送出去,或者你會抱怨。這意味着你的變量沒有正確填充。而不是僅僅發送郵件,回聲出各種變量,因爲他們正在建造,是這樣的:

echo 'Checking name'; 
if ($_POST['name'] .....) { 
    echo 'name is blank'; 
} else { 
    $name = ...; 
    echo "Found name=$name"; 
} 

基本上有你的代碼變得非常「健談」,並告訴你它正在做什麼,在每一個階段。

+0

+1對'$ _SERVER ['REQUEST_METHOD']' – stealthyninja 2011-04-01 22:10:31

0

@dafz:更改

if(isset($_POST['submit'])) { 

if(isset($_POST['enviar'])) { 

@Marc乙有好報贊成票他的回答也是如此。

編輯

你可以試試下面的更新。

if(!isset($hasError)) { 
    $siteAddress = '[email protected]'; //Put [email protected] or [email protected] your domain here 
    $emailTo = '[email protected]'; //Put your own email address here 
    $body = "Name: $name \r\nEmail: $email \r\nSubject: $subject \r\nComments: $comments \r\n"; 

    $headers = 'To: ' . $name . ' <' . $emailTo . '>' . "\r\n"; 
    $headers .= 'From: My Site <' . $siteAddress . '>' . "\r\n"; 
    $headers .= 'Reply-To: ' . $email . "\r\n"; 

    if (mail($emailTo, $subject, $body, $headers)) { 
     $emailSent = true; 
    } else { 
     $emailSent = false; 
    } 
} 
+0

嘗試過但仍然無法工作 – dafz 2011-04-02 00:57:17