How to use PHPMailer without Composer?
If you want to integrate PHPMailer into a legacy project or a simple script without setting up Composer, the immediate hurdle is understanding that the library isn't a single file. The error Fatal error: Class PHPMailer not found occurs because your require_once("src/PHPMailer.php") statement points to the wrong location.
In modern versions of the repository, the main class resides in src/PHPMailer.php, but it does not exist in isolation. To instantiate new PHPMailer, you must also load the supporting infrastructure files that define namespaces and constants. If you only include the main class file without its dependencies, the namespace mapping fails, resulting in the fatal error you encountered.
The File Structure Problem
The confusion often stems from how GitHub zip files differ from the actual repository structure on disk. When you download a release or zip directly from GitHub, it frequently excludes hidden files (those starting with a dot) and certain configuration folders unless specifically requested. This explains why autoload.php or legacy PHPMailerAutoload.php files appear missing; they are often generated by Composer or exist only in specific development branches that aren't part of the standard release download.
To get PHPMailer working via direct file inclusion without Composer, you must manually locate and include the necessary class files from the extracted src directory.
Manual Autoloading Strategy
The most robust way to use PHPMailer without Composer is to explicitly instantiate the autoloader that comes with the library itself. Even if a generic autoload.php isn't visible in your downloaded zip, the project includes a specific autoloader file designed for this exact purpose.
Look inside your extracted src folder. You should find a file named PHPMailerAutoload.php. This file contains the logic to map the PHPMailer\PHPMailer namespace to the correct class files on the filesystem.
Update your code to load both the autoloader and the main class:
// Load the autoloader first
require_once 'vendor/phpmailer/phpmailer/src/PHPMailerAutoload.php';
// Instantiate the class
$mail = new PHPMailer\PHPMailer\PHPMailer();
Note that you must also use the fully qualified namespace PHPMailer\PHPMailer\PHPMailer when creating the instance. If you attempt to instantiate it as just new PHPMailer, the autoloader won't resolve the name unless your working directory is specifically configured to treat the src folder as the root, which is rarely the case in a standard project structure.
Updating for Modern Repositories
If you cannot find PHPMailerAutoload.php in your specific download, it is likely because you downloaded a very recent version of the library. The maintainers deprecated the legacy autoloader and now recommend using Composer's PSR-4 autoloading exclusively.
However, if you are strictly avoiding Composer, you have two alternatives:
- Use an older stable release: Download a specific tag from the GitHub releases page (e.g., version 6.9.x) that still includes
PHPMailerAutoload.php. This gives you a "pure xcopy" deployment experience where you simply drop the folder into your server and include the autoloader file directly. - Manually build the namespace map: You can write a simple custom include path or use a function to manually require the specific files needed for SMTP configuration, though this is brittle and prone to breaking with minor library updates.
Best Practice Recommendation
While manual file inclusion satisfies the "no Composer" requirement, it introduces maintenance risks. Every time the PHPMailer team updates their source code on GitHub, you risk having to update your local copy manually. If a new class is introduced or a method signature changes, your hardcoded require_once statements may break silently or cause errors.
If you are building a new application or have control over the environment, using Composer is significantly safer. It handles version locking, ensuring that the exact files required for autoloading are present regardless of how the library evolves. However, for quick scripts, legacy systems, or environments where dependency managers are prohibited, loading PHPMailerAutoload.php remains the correct manual approach to bypass the Class not found error.
Always verify the contents of your downloaded zip against the GitHub source tree to ensure you haven't missed hidden configuration files that define the library's internal paths.