CodeIgniter调用emailController提示‘Unable to locate Session.php’问题排查
Alright, let's break down why you're hitting this error—even after removing the session library load line—and how to fix it properly, following CodeIgniter's MVC conventions.
The Root Cause
Your core issue is manually including emailController.php directly in your confirm() method (via include/require). CodeIgniter relies on its own class loader system to locate core libraries, helpers, and custom files. When you bypass this system by manually loading a controller, you break the framework's context: the loader can't find the correct path to core classes like Session, even if you try to load them later.
Even if you remove the session library load call from emailController's constructor, the controller itself is being loaded outside the framework's normal flow, leading to cascading path resolution issues.
Step-by-Step Fix
1. Refactor Email Logic into a Custom Library
Instead of putting email-sending logic in a controller, move it to a custom library—this is the proper MVC approach for reusable business logic.
Create a new file at application/libraries/Mailer.php:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Mailer { private $CI; // Initialize with CodeIgniter instance public function __construct() { $this->CI =& get_instance(); // Load required libraries/helpers via the framework's loader $this->CI->load->library(['session', 'form_validation', 'email']); $this->CI->load->helper(['html', 'form', 'url']); $this->CI->load->driver('cache'); } public function send_client_email($recipient, $subject, $body, $pdf_attachment_path = null) { // Configure SMTP settings (adjust these to match your provider) $email_config = [ 'protocol' => 'smtp', 'smtp_host' => 'smtp.yourprovider.com', 'smtp_port' => 587, 'smtp_user' => 'your-email@domain.com', 'smtp_pass' => 'your-email-password', 'mailtype' => 'html', 'charset' => 'utf-8', 'newline' => "\r\n" ]; $this->CI->email->initialize($email_config); // Set email details $this->CI->email->from('your-sender-email@domain.com', 'Your Business Name'); $this->CI->email->to($recipient); $this->CI->email->subject($subject); $this->CI->email->message($body); // Attach PDF if path is provided if ($pdf_attachment_path && file_exists($pdf_attachment_path)) { $this->CI->email->attach($pdf_attachment_path); } // Send and return result return $this->CI->email->send(); } }
2. Update Your confirm() Method to Use the Library
In your original controller (the one with the confirm() method), load the custom library and call its method instead of manually including another controller:
public function confirm() { // Load the custom mailer library $this->load->library('mailer'); // Prepare your email parameters $client_email = 'client@example.com'; $email_subject = 'Your Request Confirmation'; $email_body = '<h1>Thank You!</h1><p>Here is your attached document.</p>'; $pdf_path = FCPATH . 'path/to/your/attachment.pdf'; // Use FCPATH for absolute path // Send the email $email_sent = $this->mailer->send_client_email($client_email, $email_subject, $email_body, $pdf_path); // Load the confirmation view (pass result if needed) $this->load->view('confirm_msg', ['email_sent' => $email_sent]); }
3. Ditch the Manual class.smtp.php Include
CodeIgniter's built-in Email library already handles SMTP—you don't need to manually include class.smtp.php. The configuration in the custom library above is all you need to set up SMTP delivery.
Why This Works
- By using CodeIgniter's official loader system, you ensure all core libraries (like Session) are loaded from the correct paths, within the framework's context.
- Custom libraries are designed for reusable logic, making your code cleaner and easier to maintain.
- You avoid the pitfalls of manually loading controllers, which bypasses the framework's initialization process.
内容的提问来源于stack exchange,提问作者codelearner

