2014-04-12 87 views
0

我的問題是非常令人沮喪和IM拉出我的頭髮。我採取了PHP郵件功能的實例,並試圖使其適應我的需要。該示例適用於發佈到php文件的html表單。上面的例子可以在此鏈接http://www.freecontactform.com/email_form.php改編郵件功能不起作用

我採取這種確切的代碼,並試圖改變什麼,但投入的名字和他們的數量被發現。

這個例子在我的域名在線測試時非常有效,我發送給我的Gmail多次並且連續不斷地檢查它是否被垃圾郵件過濾。

我簡單的改編版本如下。

HTML表單

<!DOCTYPE html> 
    <html> 
    <head> 
    <meta charset="utf-8" /> 
    </head> 
    <body> 
    <form name="results" method="post" action="mailresultstest.php"> 
    <table> 
    <tr> 
    <td>email<input type"text" name="email" size="20"></td> 
    <td></td> 
    <td></td> 
    <td></td> 
    <td></td> 
    </tr> 
    <tr> 
    <td>HOME TEAM</td> 
    <td><input type="text" name="hometeam" size="20" maxlength="25"></td> 
    <td>VS</td> 
    <td>AWAY TEAM</td> 
    <td><input type="text" name="awayteam" size="20" maxlength="25"></td> 
    </tr> 
    <tr> 
    <td>Round 1</td>  
    <td><input type="text" name="playerhome1" size="20"></td> 
    <td><input type="text" name="playeraway1" size="20"></td> 
    <td> 
    </td> 
    </tr> 
    <tr> 
    <td><input type="submit" value="submit"> </td> 

    </table> 


    </form> 
    </body> 
    </head> 
    </html> 

,而PHP的mailresultstest.php

  <?php 

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



     // EDIT THE 2 LINES BELOW AS REQUIRED 

     $email_to = "[email protected]"; 

    $email_subject = "Match Results"; 





    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_message."<br /><br />"; 

      echo "Please go back and fix these errors.<br /><br />"; 

      die(); 

     } 



     // validation expected data exists 

     if(!isset($_POST['hometeam']) || 

      !isset($_POST['awayteam']) || 

      !isset($_POST['email']) || 

      !isset($_POST['playerhome1']) || 

      !isset($_POST['playeraway1']) || { 

      died('We are sorry, but there appears to be a problem with the form you submitted.');  

     } 

     $hometeam = $_POST['hometeam']; // 

     $email_from = $_POST['email']; // required 

     $awayteam = $_POST['awayteam']; // 

     $playerhome1 = $_POST['playerhome1']; // 

    $playeraway1 = $_POST['playeraway1']; // 




    $error_message = ""; 

    if(strlen($hometeam) < 2) { 

     $error_message .= 'The hometeam 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 .= "Home team: ".clean_string($hometeam)."\n"; 

     $email_message .= "Away team: ".clean_string($awayteam)."\n"; 

     $email_message .= "Round 1 Home player: ".clean_string($playerhome1)."\n"; 

     $email_message .= "Round 2 Away player: ".clean_string($playeraway1)."\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 

    } 

    ?> 

我適應碼本來很多更加複雜和有一些更多的輸入領域,包括選擇比什麼IM張貼在這裏,但無論我如何簡化它,當我提交表單時,php都不返回任何內容,並且不會發送電子郵件。

在我最初的改編中,我沒有使用任何字符串長度檢查或$ error_message,而我的原始表單沒有電子郵件字段,我只是將其添加回來以滿足intial if語句和標題。我試圖從php代碼中刪除該字段和其現有的並使用

if(isset ($_POST['submit'] 

但是唉,無濟於事。我誠實地只是在一個損失,爲什麼它完美的作品的例子,而不是我的版本。我真的看不出變量名稱的差異。

任何幫助,將不勝感激。在此先感謝第一

+1

哪裏是「你的版本」? – Jasper

+0

什麼ive發佈的20個變化之一,只是最基本的輸入數量方面,我知道它幾乎與鏈接中的例子相同,這就是爲什麼我不明白爲什麼它不起作用。 – OrisHeavens

+0

行!!isset($ _ POST ['playeraway1'])|| {'缺少右括號。發佈完整的錯誤消息總是有幫助的。 – Jasper

回答

0

第一件事:

更改此:

if(!isset($_POST['hometeam']) || 

     !isset($_POST['awayteam']) || 

     !isset($_POST['email']) || 

     !isset($_POST['playerhome1']) || 

     !isset($_POST['playeraway1']) || { 

     died('We are sorry, but there appears to be a problem with the form you submitted.');  

    } 

if(!isset($_POST['hometeam']) || 

     !isset($_POST['awayteam']) || 

     !isset($_POST['email']) || 

     !isset($_POST['playerhome1']) || 

     !isset($_POST['playeraway1'])) { 

     died('We are sorry, but there appears to be a problem with the form you submitted.');  

    } 

您還需要考慮的function died()。它不使用函數中任何位置的參數$error

1

我認爲你可以做到通過smtp.gmail.com發送電子郵件是Gmail的

<?php 

require_once('../classes/class.phpmailer.php'); 


$mail = new PHPMailer(true); 

$mail->IsSMTP(); 

try { 

// $mail->Host  = "mail.yourdomain.com"; // SMTP server 
// $mail->SMTPDebug = 2;      // enables SMTP debug information (for testing) NOTE:- remove comment to or for test 
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
    $mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
    $mail->Port  = 465;     // set the SMTP port for the GMAIL server 
    $mail->Username = "[email protected]"; // GMAIL username 
    $mail->Password = "yourgmailpassword";   // GMAIL passwordAIL password 
    $address = "[email protected]"; 
    $mail->AddAddress($address, "friend name"); 

    //$mail->AddReplyTo("[email protected]","your name"); 

    $mail->Subject = "Advanced gmail mailer smtp"; 


    $mail->MsgHTML(file_get_contents('contents.html')); 
    //$mail->AddAttachment('images/1.jpg');  // attachment 
    //$mail->AddAttachment('images/2.jpg'); // attachment 
    if($mail->Send()){ 
     echo "Message Sent OK</p>\n"; 
    }else{ 
     echo "Sorry some problem OK</p>\n"; 
    } 

} catch (phpmailerException $e) { 
     echo $e->errorMessage(); //error messages from PHPMailer 
} catch (Exception $e) { 
     echo $e->getMessage(); //error messages from anything else! 
} 
?> 

您可以使用您的域名的電子郵件服務器,或者如果你沒有,那麼你可以通過Gmail的SMTP服務器試試這個郵件服務器。請注意,如果您在Gmail帳戶添加兩步驗證,那麼你應該有刪除這兩步驗證發送電子郵件。

你可以從這個鏈接Here

下載我的例子,我跳上它會幫助你!