将关注另存为send.php,可以用php -f send.php
或127.0.0.1/send.php
执行,没有错误信息作为输出。
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once('/home/debian9/vendor/autoload.php');
$mail = new PHPMailer();
?>
use语句将命名空间的PHPMailer类导入全局范围,因此不需要使用FQCN(完全合格的类名),例如
$mail = new PHPMailer\PHPMailer\PHPMailer();
在这里,仅$mail = new PHPMailer();
就足够了。
使用php -a打开php shell。
php > use PHPMailer\PHPMailer\PHPMailer;
php > use PHPMailer\PHPMailer\Exception;
php > require '/home/debian9/vendor/autoload.php';
php > $mail = new PHPMailer(true);
PHP Warning: Uncaught Error: Class 'PHPMailer' not found in php shell code:1
Stack trace:
#0 {main}
thrown in php shell code on line 1
为什么$mail = new PHPMailer(true);
无法将命名空间的PHPMailer类导入php shell中的全局作用域?
您必须在php shell中编写$mail = new PHPMailer\PHPMailer\PHPMailer();
。