2012-10-02 25 views
0

我爲我的大學的俱樂部製作了一個網站,並使用了PHPMailer。當我在我自己的服務器上嘗試它時,它完美地工作。然而,在我將該網站上傳到學校的FTP後,我的PHPMailer無法正常工作。我聯繫了學校的IT部門,他們說:「服務器上的PHP版本是4.3.9,你編寫的代碼必須適合它。在服務器的錯誤日誌中,我們得到以下錯誤:PHP解析錯誤:解析錯誤,意外'{'在23行的mailer.php中「。 我檢查了我的密碼十億次,但我無法解決這個問題。這裏是我的代碼:PHPMailer給出錯誤消息:「PHP解析錯誤:解析錯誤,意外'{'in mailer.php on line 23」

<? 
if(!empty($_POST['sender_mail']) 
    || !empty($_POST['sender_name']) 
    || !empty($_POST['sender_surname']) 
    || !empty($_POST['sender_major']) 
    || !empty($_POST['sender_schoolyear']) 
    || !empty($_POST['sender_id'])) 
{ 
    phpinfo(); 
    require_once('class.phpmailer.php'); 
    include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 
    $smail = $_POST['sender_mail']; 
    $name = $_POST['sender_name']; 
    $surname = $_POST['sender_surname']; 
    $major = $_POST['sender_major']; 
    $schoolyear = $_POST['sender_schoolyear']; 
    $id = $_POST['sender_id']; 

    $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch 

    $mail->IsSMTP(); // telli ng the class to use SMTP 

    try { // Here is 23th line 
     $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 = "xxxxxxx";   // GMAIL password 
     $mail->AddAddress('[email protected]', 'Membership'); 
     $mail->SetFrom('[email protected]', 'GGK'); 
     $mail->Subject = 'New Membership'; 
     $mail->IsHTML(true); 
     $mail->Body = '<h3>New Membership</h3><br/><i><b>Name: </i></b><i>' . $name . '</i><br/><b><i>Surname: </i></b><i>' . $surname . '</i><br/><b><i>Mail: </i></b><i>' . $smail . '</i><br/><b><i>ID: </i></b><i>' . $id . '</i><br/><b><i>Schoolyear: </b></i><i>' . $schoolyear . '</i><br/><b><i>Major: </b></i><i>' . $major . '</i>'; 
     $mail->Send(); 
     echo "Message Sent OK</p>\n"; 
    } catch (phpmailerException $e) { 
     echo -1; 
     echo $e->errorMessage(); //Pretty error messages from PHPMailer 
    } catch (Exception $e) { 
     echo -1; 
     echo $e->getMessage(); //Boring error messages from anything else! 
    } 
} 
else{ 
    echo -1; 
} 
?> 

注:我得到了所有形式的值與ajax並將它們發佈到mailer.php。

+0

我的IDE說沒有錯誤,你確定這是正確的代碼\文件? – 2012-10-02 19:59:24

+1

不要相信'try','catch'或者異常模型在PHP版本5之前就存在。 – Jrod

回答

1

Try/catch僅限於PHP5版本。您需要執行其他錯誤捕獲(if/else)才能執行測試。

+0

對於PHP4中try/catch的替代方式,你有什麼建議? – Tugkan

+0

甚至更​​好的升級php版本4.3.9是非常危險的(2004年9月22日)並且不受支持「自2007-12-31以來,對PHP 4的支持已經停止,請考慮升級到PHP 5。 – 2012-10-02 20:06:23

+0

我無法升級PHP的版本。學校的IT不允許改變它。 – Tugkan