Welcome Guest, Not a member yet? Register   Sign In
Class "PHPMailer\PHPMailer\PHPMailer" not found
#1

I'm using CodeIgniter 4 and I'm trying add PHPMailer to my project via a composer:

Quote:composer require phpmailer/phpmailer

[Image: TNIYc.png]

My Controller:

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
App\Models\IssueModel;
use 
PHPMailer\PHPMailer\PHPMailer;
use 
PHPMailer\PHPMailer\SMTP;
use 
PHPMailer\PHPMailer\Exception;

class 
Issues extends Controller {

    public function create() {
        if ($this->request->getMethod() == 'post') {
            .... // some code here
                $issueModel = new IssueModel();

                $mail = new PHPMailer(true);

                $mail->SMTPDebug SMTP::DEBUG_SERVER;
                $mail->isSMTP();
                $mail->Host      'mai';
                $mail->SMTPAuth  true;
                $mail->Username  'noreply';
                $mail->Password  'pass';
                $mail->SMTPSecure PHPMailer::ENCRYPTION_SMTPS;
                $mail->Port      587;

                ..... // some code here
    }




And I have this error:

Quote:Class "PHPMailer\PHPMailer\PHPMailer" not found

Where is a problem?
Reply
#2

(This post was last modified: 08-31-2022, 07:16 AM by captain-sensible. Edit Reason: extra added )

I use phpmailer and it works a treat. Structure of my app is :


Code:
[andrew@darkstar:/srv/http]$ tree -L 1 ads.com                                                                                                                (08-31 15:08)
ads.com
├── 1.png
├── app
├── bootstrapCss
├── bootstrapS
├── builds
├── composer.json
├── composer.lock
├── env
├── fontawesome
├── Gruntfile.js
├── Gruntfile.js.bk
├── Gruntfile.js.save
├── gulpfile.js
├── license.txt
├── node_modules
├── package.json
├── package-lock.json
├── PHPMailer
├── phpunit.xml.dist
├── public
├── README.md
├── scss
├── spark
├── tecnickcom
├── tests
├── vendor
└── writable

/app/config/Autolaod.php is "

Code:
    public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        
        
        'Config'      => APPPATH . 'Config',
        'PHPMailer\\PHPMailer'=> ROOTPATH.'PHPMailer/src',
        
        
    ];


And in controller to use it :

Code:
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
use \App\Andy\CheckSpam;
use \App\Andy\Utility;
use CodeIgniter\I18n\Time;

//then to instantiate

                      $mail = new PHPMailer(true);


structure of PHPMAiler directory is:

Code:
                  

andrew@darkstar:http/ads.com]$ tree PHPMailer -L 1                                                                                                           (08-31 15:14)
PHPMailer
├── COMMITMENT
├── composer.json
├── get_oauth_token.php
├── language
├── LICENSE
├── README.md
├── SECURITY.md
├── src
└── VERSION

the classes for phpmailer are of course in source
CMS CI4     I use Arch Linux by the way 




Arch Book
Reply
#3

I added this to my app/Config/Autoload.php:

PHP Code:
'PHPMailer\PHPMailer'  => ROOTPATH 'vendor/phpmailer/phpmailer/src' 

and it works! But I have one more question.
Why you used '\\' (double) in below code?

(08-31-2022, 07:12 AM)captain-sensible Wrote:
Code:
public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace


'Config'      => APPPATH . 'Config',
'PHPMailer\\PHPMailer'=> ROOTPATH.'PHPMailer/src',
];
Reply
#4

(This post was last modified: 09-01-2022, 01:56 AM by captain-sensible. Edit Reason: elaboration of special charaxcter )

basically the first \ "escapes" the second - just a quirk of the way things work


like "@" , " | " " \ " is a special character so there is a clash between implementation of what a special character does and use as namespace path. So by adding another \ it equates to literal use
CMS CI4     I use Arch Linux by the way 




Arch Book
Reply
#5

(This post was last modified: 08-31-2022, 04:25 PM by kenjis.)

It seems your code should work.

Code:
--- a/app/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -2,10 +2,22 @@

namespace App\Controllers;

+use PHPMailer\PHPMailer\PHPMailer;
+use PHPMailer\PHPMailer\SMTP;
+
class Home extends BaseController
{
    public function index()
    {
-        return view('welcome_message');
+        $mail = new PHPMailer(true);
+
+        $mail->SMTPDebug = SMTP::DEBUG_SERVER;
+        $mail->isSMTP();
+        $mail->Host      = 'mai';
+        $mail->SMTPAuth  = true;
+        $mail->Username  = 'noreply';
+        $mail->Password  = 'pass';
+        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
+        $mail->Port      = 587;
    }
}

There is no error:

Code:
$ php public/index.php
$
Reply
#6

One thing that helps with debugging namespaces is to run from the web app :

Code:
[andrew@darkstar:http/kursaal2.org]$  php spark namespaces                                                                                                    (09-18 10:36)

CodeIgniter v4.2.6 Command Line Tool - Server Time: 2022-09-18 04:37:14 UTC-05:00

+---------------------+-------------------------------------------------------------+---------+
| Namespace           | Path                                                        | Found?  |
+---------------------+-------------------------------------------------------------+---------+
| CodeIgniter         | /srv/http/kursaal2.org/vendor/codeigniter4/framework/system | Yes     |
| App                 | /srv/http/kursaal2.org/app                                  | Yes     |
| Config              | /srv/http/kursaal2.org/app/Config                           | Yes     |
| PHPMailer\PHPMailer | /srv/http/kursaal2.org/PHPMailer/src                        | MISSING |
+---------------------+-------------------------------------------------------------+---------+


helped me track down why it wasn't working in a new web
CMS CI4     I use Arch Linux by the way 




Arch Book
Reply




Theme © iAndrew 2016 - Forum software by © MyBB