【问题标题】:PHP Fatal error: require(): Failed opening required 'vendor/phpmailer/phpmailer/src/Exception.php'PHP致命错误:require():无法打开所需的'vendor/phpmailer/phpmailer/src/Exception.php'
【发布时间】:2020-10-02 00:29:36
【问题描述】:

下面是我的 php 代码。我第一次使用 gcp 应用程序引擎,它抛出错误 PHP 致命错误:require(): Failed opening required '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>

【问题讨论】:

  • 你没有使用作曲家吗?为什么要单独包含 phpmailer 文件?此外,“要求”应放在“使用”之前。

标签: php


【解决方案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;

【讨论】:

    【解决方案2】:

    如果你通过 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

    【讨论】:

    • 我厌倦了上面的东西,它也不能正常工作。它现在抛出以下错误-“未声明 HTML 文档的字符编码。如果文档包含 US-ASCII 范围之外的字符,则该文档将在某些浏览器配置中呈现乱码。页面的字符编码必须在文档或传输协议中声明。”
    • @ra459 这不是我给你的代码有问题!这是 PHPMailer 抛出的异常。您只需要正确配置您的 PHPMailer 设置。我已经解决了您最初遇到的问题。
    • 我已经解决了上述不相关的问题,但是按照你所说的应用更改后,现在我收到了这个错误。 require():打开失败所需的'vendor/autoload.php'(include_path='.;/base/data/home/apps/j~dt-website-278313/20200612t145411.427336870951406362/;/base/alloc/tmpfs/dynamic_runtimes /php55g/6485fc5dd706e6f8/sdk')
    • Composer自动生成的autoload.php文件有什么原因无法require?我猜你没有通过 Composer 正确安装 PHPMailer 或者autoload.php 文件的路径不正确。
    • 我已经解决了这个问题。实际上 gcp 不能识别 autolaod.php 所以我不得不使用 gcp mailer。
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2020-06-28
    • 2021-07-15
    • 2019-06-07
    • 2021-03-30
    • 2021-05-16
    • 1970-01-01
    • 2018-03-05
    相关资源
    最近更新 更多