【问题标题】:PHPMailer SMTP Class not found未找到 PHPMailer SMTP 类
【发布时间】:2020-06-11 15:43:57
【问题描述】:

我不确定我做错了什么。我已逐行遵循教程,但不确定我为什么会收到此错误。我的php邮件代码如下。

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

require dirname($_SERVER['DOCUMENT_ROOT'])."/private/PHPMailer-master/src/Exception.php";
require dirname($_SERVER['DOCUMENT_ROOT'])."/private/PHPMailer-master/src/PHPMailer.php";    
require dirname($_SERVER['DOCUMENT_ROOT'])."/private/PHPMailer-master/src/SMTP.php";

// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                     
    $mail->isSMTP();                                            
    $mail->Host       = 'a2plcp473.prod.iad.secureserver.net';          
    $mail->SMTPAuth   = true;                                  
    $mail->Username   = 'ew@myemail.com';                     
    $mail->Password   = 'xxx';                            
    $mail->SMTPSecure =  'tls' ;                                
    $mail->Port       = 465;                                   

    //Recipients
    $mail->setFrom('ew@mymail.com', 'Name');
    $mail->addAddress('ew@othermail.com', 'Name');     
    $mail->addReplyTo('ew@othermail.com', 'Name');

    // Content
    $mail->isHTML(true);                                
    $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}";
}

我知道我的文件路径是正确的,因为我在每个文件中都放了一个回显“This”,它回显到屏幕上。所以路径是正确的。

我得到的错误是:

Fatal error: Uncaught Error: Class 'SMTP' not found in 
/home/xxx/public_html/public/internal/mail.php:23 Stack trace: #0 {main} thrown in 
/home/xxx/public_html/public/internal/mail.php on line 23

第 23 行是:

$mail->isSMTP(); 

我被困在这里。这两天我一直在网上寻找解决方案。任何帮助,将不胜感激。谢谢。我也知道堆栈上还有其他类似的帖子,但我已经尝试了所有这些但无法让它工作。同样,它们中的大多数是为使用作曲家的人准备的,而我没有使用作曲家,我只包括文件路径。他们中的大多数也使用最近被弃用的自动加载器。请帮忙。谢谢。

【问题讨论】:

  • 我的建议:学习使用composer。它会让你的生活更轻松。旧的 PHPMailer 自动加载器在 3 年前被弃用,而不是最近。

标签: php email phpmailer


【解决方案1】:

您缺少将 SMTP 类引入命名空间的 use 语句。将其与其他内容一起添加:

use PHPMailer\PHPMailer\SMTP;

【讨论】:

  • 添加后,我收到另一条消息:220-我们不授权使用此系统传输未经请求的 220 和/或批量电子邮件。
  • ...这意味着它运行良好!这只是邮件服务器不太友好的打招呼方式,不是任何错误。
【解决方案2】:

请查看Installation & loading:

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

如果您没有明确使用 SMTP 类(您可能没有), SMTP 类不需要使用行。

但是,您正在明确使用 SMTP 类:

$mail->SMTPDebug = SMTP::DEBUG_SERVER;    
                   ^^^^

您需要使用完全限定名称 (PHPMailer\PHPMailer\SMTP) 引用 SMTP,或者使用 use 为其别名。


如果它终于可以正常工作并且您不想看到调试消息,请删除或评论此行:

$mail->SMTPDebug = SMTP::DEBUG_SERVER;    

defaultSMTP::DEBUG_OFF

【讨论】:

  • 所以我会在其他使用行的顶部添加“使用 PHPMailer\PHPMailer\SMTP”?然后保持“$mail->SMTPDebug”行不变?
  • 是的,一件事或另一件事。你可以两者都做,但这会是多余的。
  • 添加后,我收到另一条消息:220-我们不授权使用此系统传输未经请求的 220 和/或批量电子邮件。
  • 因为您从邮件服务器获得了响应,这意味着您的代码终于可以运行了。我添加了一条关于如何关闭调试的说明。
猜你喜欢
  • 2022-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-19
  • 1970-01-01
  • 2018-05-02
  • 1970-01-01
相关资源
最近更新 更多