致命错误:找不到“PHPMailer”类

问题描述 投票:0回答:13
  • 我试过了:
    include_once('C:\Inetpub\wwwroot\php\PHPMailer\PHPMailerAutoload.php');

致命错误:在第 151 行的 C:\Inetpub\wwwroot\php\index.php 中找不到类“PHPMailer”

我将

PHPMailerAutoload.php
放在与我的脚本相同的目录中。

有人可以帮我吗?

php phpmailer
13个回答
125
投票

现在所有答案都已过时。大多数当前版本(截至 2018 年 2 月)不再具有自动加载功能,PHPMailer 应按如下方式初始化:

<?php

  require("/home/site/libs/PHPMailer-master/src/PHPMailer.php");
  require("/home/site/libs/PHPMailer-master/src/SMTP.php");

    $mail = new PHPMailer\PHPMailer\PHPMailer();
    $mail->IsSMTP(); // enable SMTP

    $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true; // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->IsHTML(true);
    $mail->Username = "xxxxxx";
    $mail->Password = "xxxx";
    $mail->SetFrom("[email protected]");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("[email protected]");

     if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
     } else {
        echo "Message has been sent";
     }
?>

16
投票

这是对 avs099 上面给出的内容的扩展,对于那些仍然有问题的人:

1.Makesure你已经安装了php_openssl.dll(否则在网上找到它并安装它);

2.转到你的php.ini;找到 extension=php_openssl.dll 启用它/取消注释

3.此时去github下载最新版本:6.0

4.将主副本解压到更适合您的路径(我建议与调用文件相同的目录)

现在将此代码复制到您的 foo-mailer.php 并使用您的 gmail stmp 身份验证呈现它。

    require("/PHPMailer-master/src/PHPMailer.php");
    require("/PHPMailer-master/src/SMTP.php");
    require("/PHPMailer-master/src/Exception.php");


    $mail = new PHPMailer\PHPMailer\PHPMailer();
    $mail->IsSMTP(); 

    $mail->CharSet="UTF-8";
    $mail->Host = "smtp.gmail.com";
    $mail->SMTPDebug = 1; 
    $mail->Port = 465 ; //465 or 587

     $mail->SMTPSecure = 'ssl';  
    $mail->SMTPAuth = true; 
    $mail->IsHTML(true);

    //Authentication
    $mail->Username = "[email protected]";
    $mail->Password = "*******";

    //Set Params
    $mail->SetFrom("[email protected]");
    $mail->AddAddress("[email protected]");
    $mail->Subject = "Test";
    $mail->Body = "hello";


     if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
     } else {
        echo "Message has been sent";
     }

免责声明:上面代码的原始所有者是 avs099,只是我的一点输入。

注意额外的:

a) (PHPMailer\PHPMailer) 命名空间:需要解决名称冲突。

b) (require("/PHPMailer-master/src/Exception.php");): 它在 avs099 的代码中丢失,因此 aProgger 遇到了问题,你需要那行来告诉邮件类异常类在哪里位于。


12
投票

自 2021-07 和 PHPMailer 6.5.0 起,如果没有作曲家,您需要按以下顺序进行包含:

$rdir = str_replace("\\", "/", __DIR__);                    //Root Dir
require $rdir.'/PHPMailer/src/Exception.php';
require $rdir.'/PHPMailer/src/PHPMailer.php';
require $rdir.'/PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

您可能需要根据您的目录结构进行调整。

其余代码按预期工作。

$mail = new PHPMailer(true);

try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
$mail->isSMTP();                                            //Send using SMTP
$mail->Host       = 'smtp.yourserver.com';                  //Set the SMTP server to send through
$mail->SMTPAuth   = true;                                   //Enable SMTP authentication
$mail->Username   = '[email protected]';                 //SMTP username
$mail->Password   = 'password';                             //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
$mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

//Recipients
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]', 'Joe User');          //Add a recipient
$mail->addAddress('[email protected]', 'Joe Doe');           //Name is optional
$mail->addAddress('[email protected]', 'Optional Name');     //Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz');               //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');          //Optional name

//Content
$mail->isHTML(true);                                        //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

11
投票

听起来好像使用该类所需的所有文件都不存在。我会重新开始:

  1. 点击页面右下方的“下载 ZIP”按钮,从 https://github.com/PHPMailer/PHPMailer 下载包。
  2. 解压缩 zip 文件
  3. 将语言文件夹、class.phpmailer.php、class.pop3.php、class.smtp.php和PHPMailerAutoload.php全部上传到你服务器上的同一个目录中,我喜欢在服务器上创建一个名为phpmailer的目录来放置所有这些都变成了。
  4. 在您的 PHP 项目中包含该类:
    require_once('phpmailer/PHPMailerAutoload.php');

9
投票

这只是命名空间。查看示例以供参考 - 您需要使用命名空间类或绝对引用它,例如:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

4
投票

我建议你考虑获得

composer
https://getcomposer.org Composer 使获取第三方库变得更加容易,并且对所有这些库都使用一个自动加载器。它还标准化了所有依赖项所在的位置,以及一些自动化功能。

下载https://getcomposer.org/composer.phar

C:\Inetpub\wwwroot\php

删除你的

C:\Inetpub\wwwroot\php\PHPMailer\
目录。

使用

composer.phar
获取phpmailer包使用命令行执行

cd C:\Inetpub\wwwroot\php
php composer.phar require phpmailer/phpmailer

完成后,它将创建一个

C:\Inetpub\wwwroot\php\vendor
目录以及所有 phpmailer 文件并生成一个自动加载程序。

接下来,在您的主项目配置文件中,您需要包含自动加载文件。

require_once 'C:\Inetpub\wwwroot\php\vendor\autoload.php';

vendor\autoload.php
将包含您使用的信息
$mail = new \PHPMailer;

有关 PHPMailer 软件包的其他信息,请访问 https://packagist.org/packages/phpmailer/phpmailer


1
投票

我遇到了同样的问题,除了细微的差别,PHPMailer 6.0 的版本,由好朋友 avs099 我知道自 2018 年 2 月以来的新版本的 PHPMailer 不支持自动加载,并且在实例化库时遇到严重问题有了MVC中的命名空间,我把代码留给需要的人。

//Controller
    protected function getLibraryWNS($libreria) {
    $rutaLibreria = ROOT . 'libs' . DS . $libreria . '.php';

    if(is_readable($rutaLibreria)){
        require_once $rutaLibreria;
        echo $rutaLibreria . '<br/>';
    }
    else{
        throw new Exception('Error de libreria');
    }
}

//loginController
    public function enviarEmail($email, $nombre, $asunto, $cuerpo){
    //Import the PHPMailer class into the global namespace
            $this->getLibraryWNS('PHPMailer');
            $this->getLibraryWNS('SMTP');
    //Create a new PHPMailer instance
            $mail = new \PHPMailer\PHPMailer\PHPMailer();
    //Tell PHPMailer to use SMTP
            $mail->isSMTP();
    //Enable SMTP debugging
    //      $mail->SMTPDebug = 0;                                       // 0 = off (for production use),  1 = client messages, 2 = client and server messages  Godaddy POR CONFIRMAR
            $mail->SMTPDebug = 1;                                       // debugging: 1 = errors and messages, 2 = messages only
    //Whether to use SMTP authentication
            $mail->SMTPAuth = true;                                     // authentication enabled
    //Set the encryption system to use - ssl (deprecated) or tls
            $mail->SMTPSecure = 'ssl';                                  //Seguridad Correo Gmail
    //Set the hostname of the mail server
            $mail->Host = "smtp.gmail.com";                             //Host Correo Gmail
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
            $mail->Port = 465;      //587;
    //Verifica si el servidor acepta envios en HTML
            $mail->IsHTML(true);
    //Username to use for SMTP authentication - use full email address for gmail
            $mail->Username = '[email protected]';
    //Password to use for SMTP authentication
            $mail->Password = 'tucontraseña';
    //Set who the message is to be sent from
            $mail->setFrom('[email protected]','Creador de Páginas Web');
            $mail->Subject = $asunto;
            $mail->Body = $cuerpo;
    //Set who the message is to be sent to
            $mail->addAddress($email, $nombre);
    //Send the message, check for errors
    if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
        return false;
    } else {
        echo "Message has been sent";
        return true;
    }

0
投票

只要阅读您所写的内容,您还需要将文件 class.phpmailer.php 添加到您的目录中。


0
投票

PHPMailerAutoload
需要与
class.phpmailer.php

在同一个文件夹中

这是我假设的

PHPMailerAutoload
代码:

 $filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';

0
投票
Just download composer and install phpMailler autoloader.php

https://github.com/PHPMailer/PHPMailer/blob/master/composer.json

once composer is loaded use below code:

    require_once("phpMailer/class.phpmailer.php");
    require_once("phpMailer/PHPMailerAutoload.php");

    $mail = new PHPMailer(true); 
            $mail->SMTPDebug = true;
            $mail->SMTPSecure = "tls";
            $mail->SMTPAuth   = true;
            $mail->Username   = 'youremail id';
            $mail->Password   = 'youremail password';
            $mail_from        = "youremail id";
            $subject          = "Your Subject";
            $body             = "email body";
            $mail_to          = "receiver_email";
            $mail->IsSMTP(); 
            try {
                  $mail->Host= "smtp.your.com";
                  $mail->Port = "Your SMTP Port No";// ssl port :465, 
                  $mail->Debugoutput = 'html';
                  $mail->AddAddress($mail_to, "receiver_name");
                  $mail->SetFrom($mail_from,'AmpleChat Team'); 
                  $mail->Subject = $subject;
                  $mail->MsgHTML($body);
                  $mail->Send();
                 $emailreturn = 200;
                } catch (phpmailerException $e) {
                  $emailreturn = $e->errorMessage();             
                } catch (Exception $e) {
                 $emailreturn = $e->getMessage();
                }
    echo $emailreturn;

希望这会奏效。


0
投票

我解决了将文件 class.phpmailer.php , class.smtp.php 复制到文件 PHPMailerAutoload.php 的文件夹中的错误,当然应该有我们将用于发送电子邮件的文件。


0
投票

命名空间很重要

我的方法没有 Composer,所以我很难知道哪一行是不必要的。

最后,只需按照说明中的指示包含名称空间的前 3 行,然后一切正常,如下所示:

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

//Load Composer's autoloader
require '../plugins/phpMLR/Exception.php';
require '../plugins/phpMLR/PHPMailer.php';
include '../plugins/phpMLR/SMTP.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

[...]

要完成调试,请记住在开始时使用这些行:

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
set_error_handler("var_dump");

希望对你有帮助


-1
投票

我有很多类似的错误。确保您的 setFrom 电子邮件地址在

$mail->setFrom()

有效
© www.soinside.com 2019 - 2024. All rights reserved.