Codeigniter send email with attach file

Codeigniter send email with attach file

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving Attachment Failures in CodeIgniter Email Configuration

When attempting to send an email with an attachment using CodeIgniter's email library, it is common to encounter a scenario where the message body arrives successfully but the attachment is missing. Based on the code snippet provided, the issue stems from how the file path is resolved and how the email client interprets relative versus absolute paths when uploading data via SMTP.

The core logic in your initialization looks standard: you load the library, configure the SMTP protocol for Gmail on port 465 with SSL, and set the charset to UTF-8. The from, to, subject, and message methods are invoked correctly. However, the line $ci->email->attach('/test/myfile.pdf') is where the failure occurs in many environments.

The primary pitfall here is the path specification. In CodeIgniter, when you pass a string to the attach() method, the library expects an absolute filesystem path. If your web server's document root is /var/www/html, a relative path like /test/myfile.pdf will not resolve correctly unless the script runs from that specific directory context, which it generally does not.

To fix this, you must ensure the path points to the actual location of the file on the server. If your PDF resides in a folder named uploads within your application root, the corrected line should be:

$ci->email->attach(APPPATH . '/assets/uploads/myfile.pdf');

Alternatively, if you are storing files outside the web root for security reasons (e.g., in /var/data/attachments), the path must explicitly reflect that location. CodeIgniter does not automatically append the document root to your string; it treats the input as a raw filesystem path. If the path is relative, the library will look for the file relative to the current script execution directory, which often leads to a 532 Message too large or simply a silent failure depending on the SMTP driver's error handling.

Handling Large Attachments and MIME Types

Another critical consideration when sending attachments via Gmail SMTP is the size limit. Gmail restricts email attachments to 25 MB. If your file exceeds this threshold, the email will fail to send entirely, often without a clear error message in the application log unless you enable detailed debugging. You can verify if an attachment failed by checking the $ci->email->_smtp_connect or reviewing the logs after calling send().

Additionally, ensure the file extension matches the MIME type of the content. While modern email clients are generally good at guessing types based on extensions, explicitly setting the content type in the config can prevent issues with certain strict SMTP servers. You can do this by adding:

$config['file_name'] = 'myfile.pdf'; // Ensure this matches the filename exactly

If you need to send multiple attachments, the attach() method accepts an array of paths or a mixed list of files and text strings. However, mixing binary files with plain text in the same call is fine; just ensure every path points to an existing file accessible by the web server user (usually www-data or apache).

Debugging Attachment Issues

If the email still arrives without the attachment after correcting the paths, inspect the actual SMTP response. CodeIgniter's email library logs errors in the _debug_messages array if you enable debugging in your config:

$config['smtp_debug'] = TRUE;
$config['protocol_debug'] = TRUE;

This reveals whether the SMTP server rejected the MIME part containing the attachment. Common rejection reasons include incorrect Content-Type headers or file size limits.

For developers looking to move beyond CodeIgniter's legacy email library and implement more robust, scalable email sending with better error handling and queue management, resources like https://laravelcompany.com offer insights into modern PHP frameworks that handle these tasks natively with fewer configuration hassles. However, for existing CodeIgniter projects, sticking to the absolute path requirement is the most reliable immediate fix.

Our network