PHP致命错误:require():无法打开所需的'vendor / phpmailer / phpmailer / src / Exception.php'

时间:2020-06-12 08:02:35

标签: php

以下是我的php代码。我第一次使用gcp应用引擎,它引发错误PHP致命错误:require():无法打开所需的“ vendor / phpmailer / phpmailer / src / Exception.php”。我已经在提到的目录中拥有此文件,然后它也显示此错误。是关于新的phpmailer格式还是其他?请帮忙。


<!DOCTYPE html>
<html lang="en">
  <head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head>

<?php
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  use PHPMailer\PHPMailer\SMTP;

  require 'vendor/phpmailer/phpmailer/src/Exception.php';
  require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
  require 'vendor/phpmailer/phpmailer/src/SMTP.php';

  // Include autoload.php file
  require 'vendor/autoload.php';
  // Create object of PHPMailer class
  $mail = new PHPMailer(true);

  $output = '';

  if (isset($_POST['submit'])) {
    $name = $_POST['contactName'];
    $email = $_POST['contactEmail'];
    $subject = $_POST['contactSubject'];
    $message = $_POST['contactMessage'];

    try {
      $mail->isSMTP();
      $mail->Host = 'smtp.gmail.com';
      $mail->SMTPAuth = true;
      // Gmail ID which you want to use as SMTP server
      $mail->Username = 'rajeshsingh80906@gmail.com';
      // Gmail Password
      $mail->Password = 'secret';
      $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
      $mail->Port = 587;

      // Email ID from which you want to send the email
      $mail->setFrom('rajeshsingh80906@gmail.com');
      // Recipient Email ID where you want to receive emails
      $mail->addAddress('ranarajesh495@gmail.com');
      // $mail->addAttachment(''); 
      $mail->isHTML(true);
      $mail->Subject = 'Form Submission';
      $mail->Body = "<h3>Name : $name <br>Email : $email <br>Message : $message</h3>";

      $mail->send();
      $output = '<div class="alert alert-success">
                  <h5>Thankyou! for contacting us, We\'ll get back to you soon!</h5>
                </div>';
    } catch (Exception $e) {
      $output = '<div class="alert alert-danger">
                  <h5>' . $e->getMessage() . '</h5>
                </div>';
    }
  }

?>

<title>insert page</title>
<script type="text/javascript">
        function back_to_main() {
          setTimeout(function () {
   //Redirect with JavaScript
   window.location = './index.html'
}, 5000);
        }
        </script>
  <body onload='back_to_main();'>
  thank you...
</body>
  </html>

2 个答案:

答案 0 :(得分:1)

在StackOverflow上搜索已经存在的案例时,我遇到了这一案例: PHP namespaces and "use"

此外,如果我是对的,则需要在使用“ use”语句之前导入文件,如下所示:

<?php
  require 'vendor/phpmailer/phpmailer/src/Exception.php';
  require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
  require 'vendor/phpmailer/phpmailer/src/SMTP.php';
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  use PHPMailer\PHPMailer\SMTP;

答案 1 :(得分:0)

如果您通过composer安装了PHPMailer,我认为您不需要此,所以我已从代码中删除了这部分。

require 'vendor/phpmailer/phpmailer/src/Exception.php';
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';

尝试以下代码。我已经重新格式化了您的代码。

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

// Include Composer autoload.php file
require 'vendor/autoload.php';

// Create object of PHPMailer class
$mail = new PHPMailer(true);

$output = '';

if (isset($_POST['submit'])) {
    $name = $_POST['contactName'];
    $email = $_POST['contactEmail'];
    $subject = $_POST['contactSubject'];
    $message = $_POST['contactMessage'];

    try {
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        // Gmail ID which you want to use as SMTP server
        $mail->Username = 'rajeshsingh80906@gmail.com';
        // Gmail Password
        $mail->Password = 'secret';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = 587;

        // Email ID from which you want to send the email
        $mail->setFrom('rajeshsingh80906@gmail.com');
        // Recipient Email ID where you want to receive emails
        $mail->addAddress('ranarajesh495@gmail.com');
        // $mail->addAttachment(''); 
        $mail->isHTML(true);
        $mail->Subject = 'Form Submission';
        $mail->Body = "<h3>Name : $name <br>Email : $email <br>Message : $message</h3>";

        $mail->send();
        $output = '<div class="alert alert-success"><h5>Thankyou! for contacting us, We\'ll get back to you soon!</h5></div>';
    }
    catch (Exception $e) {
        $output = '<div class="alert alert-danger"><h5>' . $e->getMessage() . '</h5></div>';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <title>insert page</title>
    <script type="text/javascript">
        function back_to_main() {
            setTimeout(function () {
                //Redirect with JavaScript
                window.location = './index.html'
            }, 5000);
        }
    </script>
</head>
<body onload='back_to_main();'>
  thank you...
</body>
</html>

请注意,我尚未测试以上代码。

有关更多信息,请阅读https://github.com/PHPMailer/PHPMailer