send html mail using codeigniter
Stefan Bogdanescu
Founder & Senior Architect
Mastering HTML Emails in CodeIgniter: Why Your Tags Aren't Rendering Correctly via SMTP
Sending HTML emails reliably through an SMTP server often introduces subtle complexities related to MIME types and character encoding. Many developers encounter frustrating issues where, despite setting the content as HTML, the resulting email body displays literal HTML tags (like <div>) instead of rendering as actual formatted content.
As a senior developer, I can tell you that this problem is rarely about the simple insertion of string data; it’s usually about how the email client interprets the structure and boundaries of the message when traversing the SMTP protocol. Let's dive into why this happens in CodeIgniter and how to fix it by understanding the principles behind proper email formatting.
The Root Cause: MIME, Encoding, and SMTP Headers
The core issue stems from the interaction between your application's output, PHP's mail functions, and the strict requirements of the Simple Mail Transfer Protocol (SMTP).
When you send an email via SMTP, the server expects a properly structured message that adheres to MIME (Multipurpose Internet Mail Extensions) standards. For HTML emails, this means the email must be formatted as a multipart/alternative message, containing both plain text and the HTML version.
In your example, if you are only setting $this->email->message($email_body);, you are feeding raw content into the system. If the SMTP configuration doesn't correctly handle the necessary headers that define the content type for each part of the email (the plain text part vs. the HTML part), the receiving mail client defaults to displaying the raw encoded data instead of rendering it as rich HTML.
The fact that it works fine when not using SMTP suggests that the underlying PHP mail() function, used in a simpler context, handles basic text delivery more leniently. When introducing strict SMTP requirements (like SSL/TLS connections), these stricter formatting rules become mandatory.
The Solution: Structuring Your HTML for Email Delivery
To ensure your HTML renders correctly across various email clients, you must structure the message as a proper multipart email. You need to explicitly define the content type for each part of the message.
Instead of just setting a single body, you should construct the entire email as a container holding multiple parts: one for plain text and one for the HTML version.
Here is a conceptual approach demonstrating how you would structure the data before sending it via CodeIgniter's mail library:
// 1. Define the Plain Text version (essential for accessibility)
$plain_text = "Hello world";
// 2. Define the HTML version (the rich content)
$html_content = "<div style='font-family: Arial; font-size: 16px;'>Hello World!</div>";
// 3. Construct the full email body as a multipart message
// Note: The CI library handles the final MIME wrapping, but we provide structured data.
$full_message = "Content-Type: text/plain; charset=utf-8\r\n\r\n" . $plain_text . "\r\n\r\n" .
"Content-Type: text/html; charset=utf-8\r\n\r\n" .
$html_content;
// Load and send using the library (adjusting how you pass the message)
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'user@gmail.com',
'smtp_pass' => '',
'mailtype' => 'html', // Setting this can sometimes help guide the sender
'charset' => 'utf-8',
'wordwrap' => TRUE
);
$this->load->library('email', $config);
// Instead of just passing a single string, you structure the content correctly.
// The exact method depends on your CI version and library implementation,
// but the principle is to ensure the HTML payload is correctly segmented.
$this->email->from('user@gmail.com', 'ddd');
$this->email->to(array('user@recipient.com'));
$this->email->subject('Testing Email with Proper HTML');
// Assuming your library supports passing complex content or constructing the body:
$this->email->message($full_message);
$this->email->send();
Best Practices for Robust Email Sending
When dealing with complex data like richly formatted HTML emails, relying solely on a single message parameter is often insufficient. Always prioritize creating a multipart email. This ensures compatibility across Outlook, Gmail, and other clients.
For more robust and scalable solutions, especially in larger applications, consider leveraging established frameworks. For instance, when building sophisticated backend systems, understanding how services like Laravel handle complex communication protocols provides excellent conceptual grounding for ensuring data integrity during transmission—a principle that applies equally to reliable email delivery.
By explicitly defining both the plain text and HTML parts with their respective Content-Type headers, you shift the responsibility from the system guessing the format to providing explicit instructions to the mail server, guaranteeing your message is interpreted correctly every time.
Conclusion
The confusion between seeing literal tags and rendered content when sending HTML emails via SMTP is a classic symptom of missing MIME structure. The fix lies not in changing how you write the HTML, but in ensuring the data you send adheres to the strict formatting rules required by email protocols. By structuring your message as multipart/alternative, you ensure that both plain text fallbacks and rich HTML content are delivered correctly, making your CodeIgniter email operations robust and reliable.