我不知道我做错了什么。我已经按照教程一行一行地做,但不知道为什么会出现这个错误。我的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 = '[email protected]';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls' ;
$mail->Port = 465;
//Recipients
$mail->setFrom('[email protected]', 'Name');
$mail->addAddress('[email protected]', 'Name');
$mail->addReplyTo('[email protected]', '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();
我卡在这里了 我已经在网上搜索了两天的解决方案。任何帮助将被感激。谢谢。我也知道在stack上有其他类似的帖子,但我都试过了,却无法解决。同样,他们中的大多数人都是使用composer的,而我没有使用composer,我只是包括文件路径。此外,他们中的大多数使用和自动加载器,这是最近被废止的。请大家帮忙。谢谢。
你缺少的是 use
语句来将SMTP类引入你的命名空间。将此声明与其他声明一起添加。
use PHPMailer\PHPMailer\SMTP;
请检查 安装& 装载:
<?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;
^^^^
你需要引用 SMTP
的全称(PHPMailer\PHPMailer\SMTP
)或用 use
.
如果它终于工作了,而你又不想看到调试信息,请删除或注释这一行。
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
The 默认 是 SMTP::DEBUG_OFF
.