This question already has answers here:
Class Firebase\JWT\JWT not found
(5 answers)
Closed last year.
I feel like I am almost there. (I could be wrong though)
from the root of my apache server (/usr/local/var/www)
I ran
composer require phpmailer/phpmailer
I've now got the folder system
/usr/local/var/www/vendor/phpmailer/phpmailer
In this folder, there is a src folder, and in that folder are basically all the files I think I need to include on the php page where I want to use PHPmailer.
On the PHP page where I want to use PHPMailer, I have at the top:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
and in the page I am calling
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions
I am getting an error message
Error: Class "PHPMailer\PHPMailer\PHPMailer" not found in /usr/local/var/www/xxx.php
It seems like something isn't linking up correctly.
Alex Howansky posted a comment:
You need to source composer's autoloader, typically via something like require_once 'vendor/autoload.php';.
Ken Lee a comment:
Add the three lines: require './Exception.php'; require './PHPMailer.php'; require './SMTP.php'; after the line use PHPMailer\PHPMailer\Exception; (change to the actual path for the 3 files please) , retry and see what happens
Both suggestions fixed the problem. I'm going with the vendor/autoload.php solution.
Related
This question already has answers here:
PHP class not found when using namespace
(2 answers)
Class not found in an simple PHP Script
(1 answer)
Closed 1 year ago.
I am following an tutorial on how to create a composer develop envoirement.
https://www.hostinger.com/tutorials/how-to-install-composer
I followed this but am getting the error 'Class "Timer" not found'.
Steps i took:
created a folder for the project
did the command "composer require phpunit/php-timer" on the folder. This automatically created the neccesary composer files.
created an php file called demo.php and copied the code from the tutorial (see code below)
Tried to run the php file with the command "php demo.php" and getting the error.
Code:
<?php
require __DIR__ . '/vendor/autoload.php';
Timer::start();
// your code
$time = Timer::stop();
var_dump($time);
print Timer::secondsToTimeString($time)
The classes in phpunit/php-timer are in a namespace called SebastianBergmann\Timer. Thus, you must either use the fully qualified class name:
\SebastianBergmann\Timer\Timer::start();
Or specify a use alias:
use SebastianBergmann\Timer\Timer;
Timer::start();
// ...
Time::stop();
I haven't looked in much detail but note that the docs for this package do not show static usage, so you might have to do:
use SebastianBergmann\Timer\Timer;
$timer = new Timer();
$timer->start();
// ...
$timer->stop();
I have a problem with setting up PHPMailer. It was working before, but now all of a sudden it stopped and this is the error I'm getting:
PHP Fatal error: require(): Failed opening required '../src/PHPMailer.php' (include_path='.:/opt/cpanel/ea-php53/root/usr/share/pear:/opt/cpanel/ea-php53/root/usr/share/php') in /home/pandatra/site.com/contacts_form/contact_form.php on line 9
Here is the code in contact_form.php:
<?php
include 'config.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require ''.$d['include_path'].'PHPMailer/src/Exception.php';
require ''.$d['include_path'].'PHPMailer/src/PHPMailer.php';
require ''.$d['include_path'].'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
if (isset($_POST['Send'])) {
How to fix this? Any ideas? I downloaded version 6.1.7 of PHPMailer.
The error you mentioned is that, the path in your require is getting Wrong. To avoid this type of problem , you should always use absolute path
e.g.
require __DIR__.'/PHPMailer/src/Exception.php';
require __DIR__.'/PHPMailer/src/PHPMailer.php';
require __DIR__.'/PHPMailer/src/SMTP.php';
# use "use" after include or require
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
__DIR__ is the absolute path of running file's directory.
Here's the problem:
I just replaced the old version with the new one
If you upgraded from 5.x to 6.x, you needed to read either the readme, the upgrade guide, or this question and answer that was specifically created to deal with this issue.
I am using two libraries on the same page. One is for pdf generation and one is for sending emails. But, this giving me error 500 - llease advise .
After some debugging, I found that it's phpmailer mails not working because of dompdf:
require_once 'lib/dompdf/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'lib/phpmailer/vendor/autoload.php';
use Dompdf\Dompdf;
class Pdf extends Dompdf{
public function __construct(){
parent::__construct();
}
}
By using alias:
use \PHPMailer\PHPMailer\{PHPMailer as mailerClass, Exception as mailerException}; // PHP 7+
I think the problem in your code is the duplicate autoloader. The best way is when you install both packages over composer and use the composer autoloader.
In both libs you can find an example how to install them over composer.
composer require dompdf/dompdf
composer require phpmailer/phpmailer
Then you have to include the autoloader for composer.
require 'vendor/autoload.php';
After that you can use the autoloader to load all packages. In the DomPDF you find good examples how to use.
https://github.com/dompdf/dompdf
500 errors are a bit hard to find. You should enable your error logs and check directly your logs. In the log a php error should be shown.
Having problem with upgrading phpMailer 5.xx to 6.0. Used several versions of phpmailer for years, no problems like this. I prefer to not use composer.
From upgrade guide "Alternatively, if you're not using composer, copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration and load each one manually." I prefer to not modify the include_path file on a shared host.
I used the PHPMailerAutoload.php for my 5.xx versions
I used this from the 6.0 package:
"Upgrading from PHPMailer 5.2 to 6.0
If you're not using composer, you can still load the classes manually, depending on what you're using:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/PHPMailer.php';
require 'src/SMTP.php';
No errors reported on these
$mail = new PHPMailer(true);
Fatal error: Uncaught Error: Class 'PHPMailer' not found in ///path to my code
Tried $mail = new \PHPMailer(true);
Tried lower case file names for PHPMailer.php and SMTP.php
Tried class.PHPMailer.php and class.SMTP.php; and with lowercase filenames
Look at the examples for reference - you need to either use a namespaced class or reference it absolutely, for example:
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
or
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
As came to light in your github ticket for the same question, use statements only apply in the file in which they are defined (see docs), and so your split into two files (which you didn't mention initially) breaks it. You have them split like this; in the first file:
use PHPMailer\PHPMailer\PHPMailer;
require 'src/PHPMailer.php';
and in the second file:
$mail = new PHPMailer(true);
The use statements do not apply to the second file, so the class is indeed undefined, though the requires are still valid as they are global. You need to split it like this:
require 'src/PHPMailer.php';
and:
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
For further info on upgrading to 6.0, read the upgrade guide.
I'm using this library to implement the Nexmo SMS service on my server. I required the library using Composer like so:
"require" : {
"prawnsalad/nexmo": "dev-master"
}
which I found here and followed the instructions included in the README like so:
require 'vendor/autoload.php';
use NexmoMessage;
$phone = '123456789';
$sms = new NexmoMessage(NEXMO_KEY, NEXMO_SECRET);//defined in another file
$sms->sendText($phone, 'from', "yo");//$phone is a valid number in actual case
However I keep getting the error in the title of this question. I see that Composer has imported the library successfully and I see the class and constructor for NexmoMessage, yet for some reason this error keeps happening no matter what I do. I'm not sure if this is due to an issue with the library or with how I'm using Composer. I've never had an issue with Composer in the past so I'm boggled why this is happening here. Thanks
Well, that library states that it supports PSR-4, which means they absolutely MUST use PHP namespaces, but they don't. That last commit 8 months ago https://github.com/prawnsalad/Nexmo-PHP-lib/commit/8e422c4f8f43c52acb70ebd5f9e7c6ff81a1f352 broke the autoloading. And nobody noticed up to today.
You can easily tell by looking at the source code: There is no namespace being used.
Your best action now would be to fix this issue and send a pull request! Github allows to edit files in the browser! Second best action would be to create an issue and let the authors know.