1

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?

3
  • have you tried use phpmailer\PHPMailer;? Commented Sep 12, 2017 at 14:39
  • namespace are declared as namespace nameOfSpace, use will try to import a class. Commented Sep 12, 2017 at 14:40
  • 4
    And what? Do you have autoloader or how do you include phpmailer files? Commented Sep 12, 2017 at 14:40

3 Answers 3

1
  1. 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

  1. In your PhpMailer.php include

        namespace phpmailer;
    
        class PHPMailer(){
        .....
        }
        ?>
    
  2. 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.

Sign up to request clarification or add additional context in comments.

Comments

0

Start looking into composer. Composer will help you using a single autoloader.

Download: Composer

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');

PHPMailer Github link

Comments

0

PhpMailer doesn't seem to be in a namespace.

include phpmailer/PHPMailerAutoload.php and use \PHPMailer :

<?php
require 'phpmailer/PHPMailerAutoload.php' ;
use \PHPMailer ;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.