【问题标题】:PHPMailer Gives Error Message: "PHP Parse error: parse error, unexpected '{' in mailer.php on line 23"PHPMailer 给出错误消息:“PHP 解析错误:解析错误,第 23 行的 mailer.php 中出现意外 '{'”
【发布时间】:2012-10-02 19:53:09
【问题描述】:

我为我大学的一个俱乐部制作了一个网站,我使用了 PHPMailer。当我在自己的服务器上尝试时,它运行良好。但是,在我将网站上传到学校的 FTP 后,我的 PHPMailer 无法正常工作。我联系了学校的IT,他们说:“服务器上的PHP版本是4.3.9。你写的代码必须适合它。在服务器的错误日志中我们得到以下错误:PHP Parse error: parse error, unexpected '{' 在第 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。

【问题讨论】:

  • 我的 IDE 说没有错误,你确定那是正确的代码\文件吗?
  • 不要相信trycatch或php版本5之前存在异常模型。

标签: php phpmailer parse-error


【解决方案1】:

Try/catch 仅在 PHP5 版本中。您需要进行其他错误捕获(if/else)才能进行测试。

【讨论】:

  • 您对 PHP4 中的 try/catch 的替代方式有何建议?
  • 更好地升级 php 版本 4.3.9 已经过时了(2004 年 9 月 22 日)并且不受支持 “自 2007 年 12 月 31 日起,对 PHP 4 的支持已停止。请考虑升级到 PHP 5。”
  • 我无法升级 PHP 的版本。学校的 IT 不允许更改。
  • 他们是傻瓜,但您可以在其他地方托管。仅仅因为它是一个学校俱乐部并不意味着它必须托管在学校服务器上。
  • 查看 PHPMailer 的源代码,您将无法在该版本的 PHP 上使用它。你要么必须找到一个适用于 PHP 4.3.9 的 PHPMailer 版本,要么使用 php 的内部 mail() 函数,要么击败你学校的 IT 部门升级他们极其过时的服务器。或者,正如 Dagon 所说,在其他地方托管。
猜你喜欢
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 2023-03-25
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 2015-03-09
  • 1970-01-01
相关资源
最近更新 更多