Reputation: 4888
I want to use minimal features of phpmailer
. this is my folder structure:
webroot/
- php/
- mail.php
- phpmailer/
- PHPMailer.php
in my mail.php
, i want to use the namespace:
<?php
use PHPMailer\PHPMailer;
$mail = new PHPMailer(true);
...
But I got error:
Fatal error: Class 'PHPMailer\PHPMailer' not found in /home/example/public_html/php/mail.php on line 2
How can I resolve this?
Upvotes: 1
Views: 282
Reputation: 21
Create include.php file.
$LibraryPath = "/webroot/php";
set_include_path(get_include_path() . PATH_SEPARATOR . $LibraryPath);
?>
*Where $LibaryPath is the path of your php folder which contains the phpmailer folder
In your PhpMailer.php include
namespace phpmailer;
class PHPMailer(){
.....
}
?>
In mail.php include the require so you can instantiate phpmailer\PHPMailer
require ('include.php');
use phpmailer\PHPMailer;
You can add more class inside the php folder and you can define their namespaces following #2.
You can call them in new pages by including 'include.php' and instantiating the class.
Upvotes: 1
Reputation: 2854
PhpMailer doesn't seem to be in a namespace.
include phpmailer/PHPMailerAutoload.php and use \PHPMailer :
<?php
require 'phpmailer/PHPMailerAutoload.php' ;
use \PHPMailer ;
Upvotes: 0
Reputation: 9341
Start looking into composer. Composer will help you using a single autoloader.
Put composer inside php folder.
Have a look at THIS
OR put the class inside the php project.
You need all these:
require_once('class.pop3.php');
require_once('class.phpmailer.php');
require_once('class.smtp.php');
require_once('PHPMailerAutoload.php');
Upvotes: 0