PHPMailer给出错误消息:“PHP解析错误:解析错误,第23行的mailer.php中出现意外'{'

时间:2012-10-02 19:53:09

标签: php phpmailer parse-error

我为我大学的一个俱乐部建立了一个网站,我使用了PHPMailer。当我在自己的服务器上试用它时,它完美无缺。然而,在我将网站上传到学校的FTP后,我的PHPMailer无法正常工作。我联系了学校IT部门,他们说:“服务器上的PHP版本是4.3.9。你编写的代码必须适合它。在服务器的错误日志中我们得到以下错误:PHP解析错误:解析错误,意外'{'在第23行的mailer.php中“。 我检查了我的代码十亿次,但我无法解决问题。这是我的代码:

<?
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   = "xxxxx@gmail.com";  // GMAIL username
      $mail->Password   = "xxxxxxx";            // GMAIL password
      $mail->AddAddress('xxxxx@gmail.com', 'Membership');
      $mail->SetFrom('xxxxx@gmail.com', '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。

1 个答案:

答案 0 :(得分:1)

Try / catch仅适用于PHP5版本。你需要做其他错误捕获(如果/ else)才能进行测试。