PHPMailer似乎未正确安装:未捕获的错误:类'PHPMailer \ PHPMailer \ PHPMailer

时间:2019-01-04 17:05:09

标签: phpmailer

运行代码时出现上述错误。按照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能够运行。我哪里出错了?

2 个答案:

答案 0 :(得分:0)

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

htdocs -index.php -Other_Program_to_run_with_PHPMailer.php -folder(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;
    }

?>

从那里开始,我已经复制并粘贴到服务器上,效果非常好。

答案 1 :(得分:0)

斯科特

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

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