解析错误:语法错误,第49行的文件意外结束

时间:2018-08-10 09:24:36

标签: php syntax-error

我有一个非常奇怪的错误。它说我的PHP文件在意外时结束。我的代码中没有看到任何错误。无论如何,它是来自PHPMailer教程的示例代码,因此它应该不起作用。有人可以解释吗? 我的代码:

<?php
require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;                              // Passing `true` enables exceptions
try {
    //Server settings

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.comcast.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'hmmmm@gmail.com';                 // SMTP username
    $mail->Password = 'Password';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->From = 'hmmm@gmail.com';
    $mail->FromName = 'Michael';
    $mail->addAddress('adress@gmail.com');     // Add a recipient
    $mail->addAddress('adress2.19@gmail.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
//  $mail->WordWrap = 50
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->send()

?>

1 个答案:

答案 0 :(得分:2)

您错过了最后一行的;分号-

$mail->send();

此外,您需要以}和catch块结尾try块

因此您的代码将变为-

<?php
require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;                              // Passing `true` enables exceptions
try {
    //Server settings

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.comcast.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'hmmmm@gmail.com';                 // SMTP username
    $mail->Password = 'Password';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->From = 'hmmm@gmail.com';
    $mail->FromName = 'Michael';
    $mail->addAddress('adress@gmail.com');     // Add a recipient
    $mail->addAddress('adress2.19@gmail.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
//  $mail->WordWrap = 50
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->send();
   }
   catch(Exception $e)
   {
      var_dump($e);
   }

?>