Reputation: 1
I am trying to implement phpmailer in my codeigniter 3 project so I required it in my composer.json and it created a phpmailer folder in the application/vendor folder. I then set my composer autoload in config.php as follows:
$config['composer_autoload'] = 'vendor/autoload.php';
After that I created a Phpmailer_library.php file inside the libraries folder:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Phpmailer_library
{
public function __construct()
{
log_message('Debug', 'PHPMailer class is loaded.');
}
public function load()
{
require_once(APPPATH.'vendor/phpmailer/phpmailer/src/PHPMailer.php');
require_once(APPPATH.'vendor/phpmailer/phpmailer/src/SMTP.php');
$objMail = new PHPMailer\PHPMailer\PHPMailer();
return $objMail;
}
}
Inside my SendEmail.php model I loaded the phplibrary in the constructor:
public function __construct(){
$this->load->library('Phpmailer_library');
}
But when it tries to send an email an error occurs:
So I tried to trace line 107 inside of my SendEmail.php model and this is the code snippet:
$mail = $this->Phpmailer_library->load();
I don't know why Call to a member function load() on null is occurring. Is there a mistake that i missed in my code? this error has been bugging my day.
I also try to follow the approved answer here but still did not work. How to integrate PHPMailer with Codeigniter 3
Upvotes: 0
Views: 596
Reputation: 37790
I think you're rather missing the point of composer! It doesn't just install libraries, it loads them for you too, so you shouldn't need to do anything at all to start using PHPMailer. If autoload.php
has been run already (which I would expect CI to do for you if you enable it), all you have to do is try to make an instance and the underlying class will be loaded automatically. Minimal code would be simply:
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
You should not need the loader wrapper at all.
Upvotes: 1