嗨,我目前正在工作的一个项目,其中有一个忘记密码系统。我决定使用PHPMailer向用户发送重置密码电子邮件,我通过Composer安装了它,它现在在供应商文件夹中。但是,在实现示例代码之后,PHP会抛出一个错误:
Notice: Undefined variable: mail in /opt/lampp/htdocs/capstone-admin/process/loginFunctions.php on line 98
Fatal error: Uncaught Error: Call to a member function isSMTP() on null in /opt/lampp/htdocs/capstone-admin/process/loginFunctions.php:98 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/capstone-admin/process/loginFunctions.php on line 98下面是我在loginFunctions.php中代码的一部分:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require __DIR__. '/../vendor/autoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
// More codes ......
?>我的目录结构是:

更新
,
$mail = new PHPMailer();提供了一个不吉利的图像,显示$mail = new PHPMailer();出现后的调试屏幕截图:。

发布于 2019-12-28 14:08:25
假设它是正确的vendor/autoload.php (请参阅内联注释):
new PHPMailer而不是new PHPMailer() --与此不同的是,代码片段$mail则是未知的。在$mail和=之间可能存在一些不可见的控制字符。如果它转储了什么,那么很可能是var_dump($mailÂ);如果不是那样的话.
运行composer dump-autoload --verbose.的
为了抛出/捕获异常,请构造类似于以下内容:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// relative vs. absolute path sometimes make a difference.
require __DIR__. '/../vendor/autoload.php';
// require '../vendor/autoload.php';
try {
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
} catch (Exception $e) {
die($e->getMessage());
}
?>https://stackoverflow.com/questions/59508502
复制相似问题