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/FreeLogicGate Jan 26 '26 edited Jan 26 '26
To access phpmailer, your script simply needs to have the use statements as described in their documentation. You should erase the phpmailer directory and files you downloaded, and refrain from that going forward. They don't belong mixed in with the php cli program files.
Anyplace you need access to the phpmailer classes you add the use statement(s) for the classes you need. No require or include.
Any script that you write that needs access to phpmailer or any other library you've added using compoer, will be autoloaded for you. You simply need to require "vendor/autoload.php" which composer creates and updates when you add libraries.
You do need to know the "relative" path of the location of the vendor directory, so you might need to have a statement like require "../vendor/autoload.php", if for example the script you intend to run is in {projectname}/bin.
Where you put "runable" php scripts is up to you, but by convention they typically go in either {project name}/public for "web" projects or {project name}/bin for cli programs.
The major frameworks (symfony and laravel) both have cli libraries where you get a cli application setup for you, and extend it with your own commands by following conventions for placing scripts where they tell you to (inside the src/ directory) but I would leave that to you, as both frameworks document this for you, as well as how to use composer to get the components you need to utilize them.
If it's a web project typically you have a "front controller" script you name "index.php" and that script will require the autoloader and bootstrap up routing or anything else you need. You didn't specify what you are trying to do with phpmailer, so I'm not going to guess what you're trying to do.