【问题标题】:PHPMailer seems to not install correctly: Uncaught Error: Class 'PHPMailer\PHPMailer\PHPMailerPHPMailer 似乎没有正确安装:未捕获的错误:类 'PHPMailer\PHPMailer\PHPMailer
【发布时间】:2019-01-04 17:05:09
【问题描述】:

我在运行代码时收到上述错误。按照 PHPMAiler Github 页面上的说明,我在 composer.json 中添加了 "phpmailer/phpmailer": "^6.0" 但 PHPMailer 似乎没有加载。

我尝试使用 $mail = new PHPMailer\PHPMailer\PHPMailer(true) 而不是 $Mail = new PHPMailer(true)。我去了 Composer IRC 频道,看看我的问题是不是出在 Composer 上。我也尝试在 require 'vendor/autoload.php' 中使用完整路径;声明。

// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';


//other code...

//Create a new PHPMailer instance
    $mail = new PHPMailer(true);

我希望 PHPMailer 能够运行。我哪里做错了?

【问题讨论】:

  • 你运行composer install了吗? vendor/phpmailer 中是否存在包?
  • 我没有运行安装。我使用了此页面上的说明。 github.com/PHPMailer/PHPMailer 。 vendor 下有一个 phpmailer 目录,它有子目录和文件。我在寻找一个特定的吗?
  • 使用composer require 也会安装包,但再次运行install 并没有什么坏处,所以再试一次以确保自动加载器重新生成。
  • 运行 composer require 再次做到了。我不知道为什么。这在暂存时有效,但我在生产中遇到了同样的问题,并且没有多少重新安装可以修复它。还有其他想法吗?

标签: phpmailer


【解决方案1】:

当我在本地 XAMPP 服务器和 composer 上运行 PHPMailer 时遇到困难时,我最终通过将文件加载到我的目录中来手动安装该文件

htdocs -index.php -Other_Program_to_run_with_PHPMailer.php -文件夹(PHPMailer)

    <?php

    $messege=""

    require 'PHPMailer/PHPMailerAutoload.php';

    $mail = new PHPMailer;

    //$mail->SMTPDebug = 3;                               // Enable verbose debug output

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

    $mail->setFrom('insert username', 'Information');
    $mail->addAddress('who it is being sent to', 'PHP Management');     // Add a recipient
    $mail->addReplyTo('who they can reply to', 'PHP Management');

    $mail->isHTML(true);                                  // Set email format to HTML

    $mail->Subject = ;
    $mail->Body    = $message;

    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'The following Message has been sent';

        echo $message;
    }

?>

从那里我一直复制并粘贴到我的服务器上,效果很好。

【讨论】:

    【解决方案2】:

    斯科特

    如果您阅读了文档,您的问题可能会得到解决。首先使用下面的代码。

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require 'path/to/PHPMailer/src/Exception.php';
    require 'path/to/PHPMailer/src/PHPMailer.php';
    require 'path/to/PHPMailer/src/SMTP.php';
    

    【讨论】:

      【解决方案3】:

      只需从该 GIT 存储库链接下载 phpMailer 类

      https://github.com/PHPMailer/PHPMailer
      

      并提取/复制三个文件 PHPMailer.php、Extension.php 和 SMTP.php 文件将它们放在一个文件夹名称中:(PHPMailer),如下图所示:- https://i.stack.imgur.com/T6DXW.png

      只需在文件顶部添加这些代码行

      //Import PHPMailer classes into the global namespace
      //These must be at the top of your script, not inside a function
      use PHPMailer\PHPMailer\PHPMailer;
      use PHPMailer\PHPMailer\SMTP;
      use PHPMailer\PHPMailer\Exception;
      
      //Load Composer's autoloader
      //  require 'vendor/autoload.php';
      require('PHPMailer/Exception.php');
      require('PHPMailer/SMTP.php');
      require('PHPMailer/PHPMailer.php');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-23
        • 1970-01-01
        • 2021-06-03
        • 1970-01-01
        • 2010-09-24
        • 2023-03-30
        相关资源
        最近更新 更多