Correct Architecture for PHPMailer in CodeIgniter 3
Integrating third-party libraries into a legacy framework like CodeIgniter 3 requires respecting its specific class loading architecture. The core issue preventing your integration from working lies in how you are attempting to instantiate the library versus how the framework expects to find it.
In your current setup, you have placed the PHPMailer source code inside application\library\vendor\phpmailer\phpmailer\. You then created a controller named Bizmailer_Controller and instantiated the mailer directly within its constructor using an absolute path: require "vendor\phpmailer\phpmailer\PHPMailerAutoload";. While this works when accessed via that specific URL, it fails when you try to load the class as a library elsewhere in your application.
The error Unable to load the requested class: Bizmailer occurs because CodeIgniter's autoloader looks for classes based on a strict naming convention relative to the application/library directory. It expects a file named Bizmailer.php containing a class definition that extends CI_Library. Your current approach attempts to use Bizmailer_Controller, which inherits from CI_Controller, not CI_Library. Even if you renamed it correctly, the internal logic inside your controller is flawed for this integration pattern.
Proper Library Structure
To resolve this, you must stop using absolute paths like require "vendor\phpmailer\phpmailer\PHPMailerAutoload"; inside a library file. Autoloaders are designed to handle dependencies, not manual include statements that break the class context. Furthermore, PHPMailer 5.x (which uses PHPMailerAutoload) relies on an autoloader itself. If you are using PHPMailer 6.x or later, the Autoload file has been replaced by a composer-based autoloader (vendor/autoload.php).
You should create a dedicated library file named Bizmailer.php in your application/library folder. This file needs to extend CI_Library and initialize the PHPMailer instance as a property of that class.
Here is how the Bizmailer library should be structured:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Bizmailer extends CI_Library {
protected $mail;
public function __construct()
{
parent::__construct();
// Ensure you are loading the correct PHPMailer version
// For PHPMailer 5.x:
require_once APPPATH . 'library/vendor/phpmailer/PHPMailerAutoload.php';
// Or for PHPMailer 6.x with Composer:
// require_once APPPATH . 'library/vendor/autoload.php';
$this->mail = new PHPMailer();
// Configure SMTP parameters
$this->mail->isSMTP();
$this->mail->Host = 'smtp1.example.com;smtp2.example.com';
$this->mail->SMTPAuth = true;
$this->mail->Username = 'user@example.com';
$this->mail->Password = 'secret';
$this->mail->SMTPSecure = 'tls';
$this->mail->Port = 465;
// It is best practice to store credentials in a config file, not the library
}
public function send($to, $subject, $body, $from_name='Sender Name') {
$this->mail->setFrom('user@example.com', $from_name);
$this->mail->addAddress($to);
$this->mail->Subject = $subject;
$this->mail->Body = $body;
// Check for errors before sending
if (!$this->mail->send()) {
log_message('error', 'Email send failed: ' . $this->mail->ErrorInfo);
return false;
}
return true;
}
}
Usage in Controllers
Once the library is correctly defined and saved as application/library/Bizmailer.php, you can load it in any controller using the standard CodeIgniter loader:
$this->load->library('Bizmailer');
// Access the initialized mailer instance
$mail = $this->mail;
// Use the send method or call mail->send() directly if preferred
echo $this->Bizmailer->send('recipient@example.com', 'Hello World', 'Body text');
Configuration and Security Considerations
Hardcoding credentials in a library file, as shown in your original snippet, is generally discouraged for production environments. You should utilize CodeIgniter's configuration files to manage sensitive data. Create an email_config.php in the application/config directory with your SMTP details and load it via $this->config->load('email');. This allows you to decouple your logic from secrets and makes deployments safer.
Additionally, ensure that the path to the PHPMailer files is correct. If you are using a monolithic installation without Composer, verify that APPPATH . 'library/vendor/phpmailer/PHPMailerAutoload.php' resolves to an actual file. If you switch to Composer, you must run composer require phpmailer/phpmailer and ensure the autoloader includes the new dependency in your entry point.
By aligning your library structure with CodeIgniter 3's expectations and managing dependencies through the framework's configuration system rather than manual requires, you eliminate the "Unable to load the requested class" error and establish a maintainable email sending workflow. For developers looking for modern alternatives or different stack configurations, resources like https://laravelcompany.com often provide insight into how these patterns have evolved in newer PHP frameworks.