PHPMailer 给出异常文件未找到

PHPMailer giving Exception file not found

所以我只是输入了以下通过 PHPMailer 发送邮件的正常代码:

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

require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'email';
$mail->Password = 'password';
$mail->SMTPsecure = 'tls';
$mail->Port = 587;
$mail->setFrom('email','password');
$mail->addReplyTo('email','npassword');
$mail->addAddress('email');
$mail->isHTML(true);
$mail->Subject = 'This is a test email sent with help of PHP Code..//';
$bodyContent = '<h1>Sending Email Through PHP..//</h1>';
$mail->Body = $bodyContent;
if(!$mail->send()){
    echo "Message Not Sent Due to the following Error:".$mail->ErrorInfo();
}
else
{
    echo "Message Sent Successfully..//";
}
?>

以及以下错误:

Warning: require(PHPMailer/Exception.php): Failed to open stream: No such file or directory in C:\xampp\htdocs\TYBSC\Tri 8\practice\MailPractice\mailer.php on line 6

Fatal error: Uncaught Error: Failed opening required 'PHPMailer/Exception.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\TYBSC\Tri 8\practice\MailPractice\mailer.php:6 Stack trace: #0 {main} thrown in C:\xampp\htdocs\TYBSC\Tri 8\practice\MailPractice\mailer.php on line 6

怎么办?? 是的,我在 htdocs 文件夹中包含了 PHPMailer 文件夹,所以我不应该有任何问题。

你的路径不正确 尝试使用这个

require './PHPMailer/Exception.php';

需要'./PHPMailer/PHPMailer.php'; 要求 './PHPMailer/SMTP.php';

如果您不使用作曲家,您可以手动加载 类。添加前检查文件路径。试试这个

require(__DIR__."/PHPMailer/src/PHPMailer.php");
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
$mail->isSMTP();
.....