r/PHPhelp • u/ww1223 • Jan 23 '26
Using PHPMailer: what is the difference between 'use' and 'require' statements?
I’ve recently started to experiment with PHPMailer to send code-generated emails via SMTP – successfully, to my surprise.
I copied the PHPMailer folder (downloaded from github) manually to my php folder, previously installed via Homebrew. I then copied some code examples from a PHPMailer tutorial, thus:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '/opt/homebrew/etc/php/8.4/PHPMailer/src/Exception.php';
require '/opt/homebrew/etc/php/8.4/PHPMailer/src/PHPMailer.php';
require '/opt/homebrew/etc/php/8.4/PHPMailer/src/SMTP.php';
… and after that used more example code snippets involving the $mail variable to configure and authenticate SMTP, compose the email and send it. I’ve omitted these as they contain my personal logins etc, and I don’t think they’re relevant to my question.
All of this works, but I’d like to understand more instead of just blindly having to follow the 'recipe'. In particular, although I understand the three ‘require’ statements which are obviously pointing to php libraries that were installed as components of the PHPMailer package, I don't understand the meaning or purpose of the two ‘use’ statements. I suspected they were associated with a Windows installation, since they contain backslashes, and were therefore redundant for my macOS installation. But if I comment them out then the code no longer works. They don’t seem to relate to any valid path set up on my machine (unlike the ‘require’ statements). Many thanks if anyone can help, and please note my level of php knowledge is beginner at best (which might be apparent from my question anyway).
Mac Mini M4
macOS Sequoia 15.5
PHP 8.4 installed via Homebrew
View all comments
Show parent comment
1
u/ww1223 Jan 30 '26
Thanks very much for these full explanations, and sorry it’s taken me a while to get back here to read them. As you may have gathered, I’m completely out of my depth and it’s clear I have much work to do on my incorrect mental model of what php is and how it works. I understand your analogy with a word processor, but I’d previously understood phpmailer to be more equivalent to an extension to the word processor’s capabilities (or a template maybe, if it’s not stretching the analogy so far), than a 'document'. So I thought sticking it close to php itself didn’t seem too inappropriate!
I saw some references to Composer (none as clearly explained as yours) but I couldn’t face another learning curve associated with yet another package seemingly required to ‘do’ php after the very painful sequence of installing and configuring first Homebrew, then php itself, PHPStorm, Xdebug, each of which took several days to identify and (partially) understand enough to get them working. Modern macOS no longer includes php, unfortunately.
As to my application, it’s simply to send emails (to myself) when a particular webpage changes. So it’s purely a local thing on the Mac, which also causes confusion since so much of php installation guidance seems geared to getting it working with a web server, or even remotely, which doesn’t apply to me.