致命错误:找不到类'PHPMailer'

ior*_*ori 21 php phpmailer

  • 我试过了 :include_once('C:\Inetpub\wwwroot\php\PHPMailer\PHPMailerAutoload.php');

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

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

有人可以帮我弄这个吗 ?

avs*_*099 76

现在所有的答案都已过时了.最新版本(截至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("xxxxxx@xxxxx.com");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("xxxxxx@xxxxx.com");

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

  • `require(“ / home / site / libs / PHPMailer-master / src / Exception.php”);`需要为第一行 (3认同)

GTo*_*rov 16

截至 2021 年 7 月和 PHPMailer 6.5.0,如果没有 Composer,您需要按以下顺序进行包含:

$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;
Run Code Online (Sandbox Code Playgroud)

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

其余代码按预期工作。

$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   = 'no-reply@example.com';                 //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('mail@example.com');
$mail->addAddress('mail@example.com', 'Joe User');          //Add a recipient
$mail->addAddress('mail@example.com', 'Joe Doe');           //Name is optional
$mail->addAddress('mail@example.com', 'Optional Name');     //Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

//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}";
}
Run Code Online (Sandbox Code Playgroud)


Dre*_*rew 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');


Har*_*ava 8

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

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

//Load composer's autoloader
require 'vendor/autoload.php';
Run Code Online (Sandbox Code Playgroud)


Ric*_*haw 8

对于仍然有问题的人,这是对avs099上面给出的扩展的回答:

1,确保已经安装了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 = "foo@gmail.com";
    $mail->Password = "*******";

    //Set Params
    $mail->SetFrom("foo@gmail.com");
    $mail->AddAddress("bar@gmail.com");
    $mail->Subject = "Test";
    $mail->Body = "hello";


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

免责声明:上面代码的原始所有者是avs099,仅提供我的少量输入。

注意其他事项:

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

b)(require(“ / PHPMailer-master / src / Exception.php”);):在avs099的代码中丢失,因此aProgger遇到了问题,您需要该行告诉mailer类Exception类的位置。


Cal*_*n K 0

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