Search code examples
codeignitercomposer-phpphpmailer

Codeigniter Composer Phpmailer


I'm struggling with Codeigniter + PHPMailer via Composer. I'm getting this error:

Class 'PHPMailer' not found

I have CI version 3.1.5 and I've composed PHPMailer 6.0.0 using the following command:

composer require phpmailer/phpmailer

Inside my root folder, so it created something like this:

/CI_root
 |-- application
 |-- system
 |-- vendor
 |   |-- phpmailer
 |       |-- phpmailer
 |           |-- language
 |           |-- src
 |-- composer.json

Inside my application/config/config.php there is this line

$config['composer_autoload'] = FCPATH."vendor/autoload.php";

PS.: I tried changing to $config['composer_autoload'] = "./vendor/autoload.php"; as well

And finally, the root composer.json

{
"description": "The CodeIgniter framework",
"name": "codeigniter/framework",
"type": "project",
"homepage": "https://codeigniter.com",
"license": "MIT",
"support": {
    "forum": "http://forum.codeigniter.com/",
    "wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
    "irc": "irc://irc.freenode.net/codeigniter",
    "source": "https://github.com/bcit-ci/CodeIgniter"
},
"require": {
    "php": ">=5.3.7",
    "phpmailer/phpmailer": "^6.0",
    "mpdf/mpdf": "^6.1"
},
"suggest": {
    "paragonie/random_compat": "Provides better randomness in PHP 5.x"
},
"require-dev": {
    "mikey179/vfsStream": "1.1.*",
    "phpunit/phpunit": "4.* || 5.*"
}

}

And inside the controller

public function index()
{
    $mail = new PHPMailer();
    // other of stuff

Funny thing is mpdf loads okay. Am I missing something here?

Thanks in advance!


Solution

  • Composer auto-loading should just be TRUE:

    $config['composer_autoload'] = TRUE;
    

    This is because CodeIgniter already knows that your composer autoloaded files will be in /application/vendor

    at the top of your file where you want to use PHP mailer, probably need something like:

    use PHPMailer;
    

    Actually, in the PHPMailer docs, they show:

    //Import PHPMailer classes into the global namespace
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;