Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer install without Composer

Tags:

php

phpmailer

Please forgive my ignorance. I am trying to install PHPMailer 6.0.1 under PHP 5.6 on Linux. My PHP installation is remote and I manage all my websites’ PHP via FTP (I typically download packages as .zips to Win 10, unpack and then FTP the result to my webspace).

Of the various ways to install PHPMailer, Composer is preferred, but this is where I come unstuck. None of the Composer instructions seems appropriate to this way of working – the installer wants me to the ‘Choose the command line PHP you want to use’, but I don’t have PHP locally ... Annoyingly, I see PHPMailer’s composer.json file installed waiting to be used. But no PHPMailerAutoload.php (is this created by Composer?)

So I try to do a manual install of PHPMailer. I download, unzip and FTP upload the resulting directories to my webspace in folder PHPMailer. I then insert the following at the head of my PHP code and outside of any functions:

require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

With the ‘use’ statements I get a syntax error unexpected 'use' (T_USE) … Without them I get as far as trying to instantiate: $mail = new PHPMailer; but this fails with a ‘class 'PHPMailer' not found

What please am I doing wrong and how can I do better?

like image 737
decomplexity Avatar asked Dec 08 '22 17:12

decomplexity


2 Answers

This isn't specific to PHPMailer - it's what you need to do to use any of the myriad PHP packages that use namespaces. The PHP docs on how to use use are here.

The short version is, you need to put namespace and use directives before any other scripting, so if you simply reverse the order of your commands, it should work:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';

Incidentally, this is the order used in the example in the PHPMailer readme and all the other examples provided with PHPMailer. You may find the upgrade guide useful too.

The PHPMailerAutoload.php file no longer exists - composer's autoloader does a much better job. PHPMailer's own composer.json file is used to resolve dependencies and flag compatibility requirements for your app's own composer file, that is, it's used to tell your project's composer file how to use PHPMailer – but is not your project's composer file itself – every package you load will have its own.

Developing without a local PHP instance is hard work – developing on your live server is, shall we say, "discouraged"! If you can't install PHP directly, run it in a container or VM using Docker, VirtualBox or something like XAMPP that's completely self-contained.

like image 186
Synchro Avatar answered Dec 10 '22 23:12

Synchro


In version 6.02, each of the phpmailer modules contain the namespace PHPMailer\PHPMailer declaration so the following works (no autoloader needed but this routine should be in /src folder):

include($_SERVER['DOCUMENT_ROOT'].'/path_setup.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/PHPMailer/src/PHPMailer.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/PHPMailer/src/SMTP.php');
require_once ($_SERVER['DOCUMENT_ROOT'].'/php/PHPMailer/src/Exception.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
like image 45
DBStamps Avatar answered Dec 10 '22 22:12

DBStamps