Codeigniter 3 Email issue

Codeigniter 3 Email Attachment Issue: How We Fixed the Multipart MIME Error

Having problems sending emails with PDF attachments in CodeIgniter? Learn how we fixed a multipart MIME formatting issue that caused raw email headers and MIME boundaries to appear instead of a properly formatted message and attachment.

CODEIGNITER TROUBLESHOOTING

CodeIgniter Email Issue With Attachments: Multipart MIME Error and Fix

Are CodeIgniter emails working correctly until you add a PDF or other attachment, only to receive a confusing multipart MIME error? This article explains a CodeIgniter email attachment issue we encountered, why the message was being displayed incorrectly, and the configuration change that resolved it.

The CodeIgniter Email Attachment Problem

One of the more confusing problems when working with the CodeIgniter email library occurs when an email sends successfully but the recipient does not see the expected HTML message or PDF attachment correctly. Instead, the email client may display raw MIME information or show a message such as:

This is a multi-part message in MIME format.

Your email application may not support this format.

In the case we investigated, the application was generating an email containing a PDF invoice. The email was intended to provide a simple message and allow the recipient to download the invoice, but the resulting email contained multipart MIME boundaries instead of being rendered normally.

The issue was related to how line endings were being generated by the CodeIgniter email library when constructing the multipart message.

What the Broken Email Looked Like

Instead of receiving a normal email containing the message and PDF attachment, the recipient could see content similar to the following:

This is a multi-part message in MIME format.

Your email application may not support this format.

--B_ATC_663a4b4ba35f

Content-Type: multipart/alternative;
boundary="B_ALT_663a4b4ba35f4"

--B_ALT_663a4b4ba35f4

Content-Type: text/plain; charset=UTF-8

Content-Transfer-Encoding: 8bit

Please Download PDF invoice

Seeing MIME headers such as Content-Type, Content-Transfer-Encoding, and multipart boundaries directly in the email is a strong indication that the email structure is not being interpreted correctly by the receiving mail client.

This can be particularly noticeable when the email contains multiple MIME sections, such as an HTML body combined with a PDF attachment.

Why Does This Happen?

Email messages containing attachments are constructed using MIME multipart formatting. The message is separated into different sections, such as the text or HTML body and the attached file.

These sections depend on correctly formatted MIME boundaries and line endings. If the email library generates an incorrect line-ending sequence, the receiving mail server or email application may fail to parse the message as intended.

This becomes especially important when CodeIgniter is generating multipart messages containing attachments such as PDF invoices, reports, documents, or other files.

In our case, changing the line-ending configuration in the CodeIgniter email library resolved the attachment and MIME formatting problem.

CodeIgniter Email Library Configuration

The relevant configuration was located in the CodeIgniter email library:

/system/libraries/email.php

The email library contained newline and CRLF configuration values. The working configuration required both values to use the standard carriage-return and line-feed sequence:

Correct Configuration

public $newline = "\r\n";
public $crlf    = "\r\n";

After changing these values, the email library generated the multipart email using the expected CRLF line endings.

The Fix

If you are experiencing the same CodeIgniter email attachment problem, locate the email library configuration and check the newline and CRLF values.

In the affected CodeIgniter installation, the configuration was changed to:

public $newline = "\r\n";
public $crlf    = "\r\n";

After applying the change, the application was able to generate the multipart email correctly and the PDF attachment was handled as expected.

It is important to test the result with the actual mail server and recipient email clients because MIME handling can also be affected by server configuration, mail transfer agents, and recipient-side filtering.

Why CRLF Matters for Email Messages

Internet email protocols traditionally use CRLF, represented in PHP as "\r\n", for separating lines in email headers and MIME sections.

A multipart email can contain several sections separated by MIME boundaries. If those boundaries or headers do not use the expected formatting, a receiving application may expose the raw message instead of displaying the email normally.

This is why a seemingly small change to the CodeIgniter email library can resolve an email attachment problem that otherwise looks like a PDF-generation, SMTP, or mail-server issue.

Check These Things Before Changing the Email Library

Although the newline configuration fixed the issue in this case, it is important to troubleshoot CodeIgniter email problems systematically.

  • Confirm that the email is actually being sent.
  • Check the SMTP configuration.
  • Verify that the attachment file exists.
  • Check the attachment path and file permissions.
  • Confirm the PDF can be opened independently.
  • Check the generated email headers.
  • Test both plain-text and HTML email content.
  • Test the message with and without attachments.
  • Check the receiving mail server and email client.
  • Review CodeIgniter email library configuration.

If the email works without an attachment but starts displaying raw MIME content when an attachment is added, the multipart email structure should be one of the first areas to investigate.

CodeIgniter PDF Email Attachments

PDF invoices are a common reason ecommerce and business applications need to generate multipart emails. An application may generate an invoice dynamically, attach the resulting PDF, and send it to a customer after an order is completed.

A typical workflow looks like this:

Order Completed
       ↓
Generate PDF Invoice
       ↓
Attach PDF
       ↓
Build Multipart Email
       ↓
Send Email
       ↓
Customer Receives Invoice

If the multipart message is incorrectly formatted, the customer may receive MIME headers and boundaries instead of a normal email.

What If the Fix Does Not Work?

If changing the newline and CRLF configuration does not resolve the problem, the issue may be elsewhere in the email delivery chain.

Check the CodeIgniter email configuration, SMTP server, generated message headers, attachment path, MIME type, PDF generation process, and receiving mail server. It is also useful to test the same message with different email clients.

For older CodeIgniter applications, also consider whether the email library has been modified locally or whether custom changes have been made to the framework files.

Conclusion

A CodeIgniter email attachment problem can sometimes appear to be an SMTP or PDF issue when the actual problem is the formatting of the multipart MIME message.

In the case described here, the solution was to update the CodeIgniter email library so that both the newline and CRLF values used the "\r\n" line-ending sequence:

public $newline = "\r\n";
public $crlf    = "\r\n";

This corrected the multipart email formatting and allowed the PDF attachment to be delivered correctly.

Frequently Asked Questions

Why does CodeIgniter show MIME content instead of my email?

If a CodeIgniter email containing an attachment displays MIME boundaries and headers, the multipart message may not be formatted or interpreted correctly. Check the email library's newline and CRLF configuration as well as the SMTP and receiving mail environment.

Why does my CodeIgniter email work without an attachment?

Attachments require a multipart MIME message. If the email works without an attachment but fails when a PDF or other file is added, investigate the multipart structure, MIME boundaries, line endings, attachment encoding, and email headers.

What should CodeIgniter email CRLF be?

In the configuration involved in this particular issue, the working values were:

public $newline = "\r\n";
public $crlf    = "\r\n";

Can this problem affect PDF invoice emails?

Yes. PDF invoices are commonly sent as email attachments, which requires multipart MIME formatting. A problem with that formatting can cause the recipient to see raw MIME content instead of a normally rendered email.

Having Problems With a CodeIgniter Application?

If your CodeIgniter application has email, attachment, API, ecommerce, database, or compatibility problems, our developers can investigate the existing application and identify the underlying issue.

Hire CodeIgniter Developers

Have a Similar Project in Mind?

We help businesses with custom web development, eCommerce solutions, integrations and digital transformation.