首页 > 解决方案 > 包含类时未找到 PHPMailer 类

问题描述

我已经从 github 安装了 PHPMailer 作为下载 master.zip

当我在下面使用它时

<?php

require "PHPMailer.php"; //include phpmailer class

// Instantiate Class
$mail = new PHPMailer();

// Set up SMTP
$mail->IsSMTP();                // Sets up a SMTP connection
$mail->SMTPAuth = true;         // Connection with the SMTP does require authorization
$mail->SMTPSecure = "ssl";      // Connect using a TLS connection
$mail->Host = "smtp.gmail.com";  //Gmail SMTP server address
$mail->Port = 465;  //Gmail SMTP port
$mail->Encoding = '7bit';

我得到以下

ubuntu@ip-172-31-35-156:/opt/PHPMailer-master/src$ php test.php
PHP Fatal error:  Uncaught Error: Class 'PHPMailer' not found in /opt/PHPMailer-master/src/test.php:6
Stack trace:
#0 {main}
  thrown in /opt/PHPMailer-master/src/test.php on line 6
ubuntu@ip-172-31-35-156:/opt/PHPMailer-master/src$ 

我究竟做错了什么?

附上文件夹结构 在此处输入图像描述

标签: phpphpmailermailer

解决方案


PHPMailer 默认位于 PHPMailer\PHPMailer 命名空间中,您可以使用use关键字将其带入全局命名空间。例如在他们的simple_contact_form示例中:

<?php
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;

推荐阅读