PHP 致命错误:require(): 需要打开失败 'vendor/phpmailer/

下面是我的 php 代码。我是第一次使用 gcp 应用程序引擎,它抛出错误 PHP Fatal error: require(): Failed opening required 'vendor/phpmailer/phpmailer/src/Exception.php'。我已经在提到的目录中有这个文件,然后它也显示这个错误。是关于新的 phpmailer 格式还是其他什么?请帮忙。


<!DOCTYPE html>

<html>

  <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>';

    }

  }

蝴蝶不菲
浏览 458回答 3
3回答

湖上湖

如果我是正确的,您需要在使用“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;

qq_遁去的一_1

如果您通过 composer 安装 PHPMailer,我认为您不需要这个,所以我已从您的代码中删除了这部分。require 'vendor/phpmailer/phpmailer/src/Exception.php';require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';require 'vendor/phpmailer/phpmailer/src/SMTP.php';试试下面的代码。我已经重新格式化了你的代码。<?phpuse PHPMailer\PHPMailer\PHPMailer;use PHPMailer\PHPMailer\Exception;use PHPMailer\PHPMailer\SMTP;// Include Composer autoload.php filerequire '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><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>请注意我没有测试上面的代码。

哈士奇WWW

如果您使用的是 linux 并且在没有作曲家的情况下下载它,请授予 PHPMailer 文件夹的权限。sudo&nbsp;chmod&nbsp;-R&nbsp;777&nbsp;youphpmailerfolder在我的例子中,我将文件夹作为 PHPMailer 放在我的根项目中名为 config 的文件夹中。所以,我所做的是从项目中输入我的文件夹配置并运行:sudo&nbsp;chmod&nbsp;-R&nbsp;777&nbsp;PHPMailer我希望这有帮助!
打开App,查看更多内容
随时随地看视频慕课网APP