2026-07-15

How to send HTML email

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

How to send HTML email

Mastering Email Delivery: How to Send Rich HTML Emails

Sending emails is a fundamental task for any application, whether you are building a web service or a mobile utility. While sending simple plain text messages using Android Intents is straightforward, transitioning to sending rich, beautifully formatted HTML emails introduces complexity related to MIME structuring and encoding. As senior developers, we need solutions that are robust, platform-agnostic, and reliable.

The initial confusion often stems from trying to force an email structure into a simple system intent. Let’s dive into why setting setType("text/html") fails in many scenarios and explore the correct, professional methods for delivering HTML content.

The Pitfall of Simple Intents

You correctly observed that attempting to use standard Android Intents directly for complex MIME types like text/html often leads to failure or incomplete delivery. Intent actions are designed primarily for launching system apps or sharing basic data. When you try to pass raw HTML strings through these mechanisms, the receiving mail client doesn't receive the necessary container structure—specifically, the crucial multipart/alternative wrapper that allows it to choose between plain text and rich text versions cleanly.

In professional application development, especially when dealing with complex content formats like HTML, relying solely on platform-specific intent handling is brittle. We need a dedicated mechanism for constructing the email itself.

The Developer's Solution: Building the MIME Message

The correct approach to sending true HTML emails involves building a complete MIME (Multipurpose Internet Mail Extensions) message. This means structuring your data so that the email server understands exactly what content is being sent and how it should be rendered by the recipient’s client.

Instead of trying to use simple intents, developers usually implement this logic on the backend or within a dedicated service layer.

Step 1: Constructing the Multipart Body

An HTML email must be structured as a multipart/alternative message. This structure allows the client (like Gmail or Outlook) to see multiple versions of the same content (plain text and HTML) and choose the best one based on their settings and device capabilities.

Here is a conceptual look at what the body construction involves, often managed by a backend language:

// Conceptual PHP/Server-side representation for email construction
$html_content = "<h1>Hello World!</h1><p>This is my beautifully formatted HTML email.</p>";
$plain_text_content = "Hello World!\nThis is my beautifully formatted plain text email.";

$message = "MIME-Version: 1.0\r\n";
$message .= "Content-Type: multipart/alternative\r\n";
$message .= "Boundary=" . md5(uniqid()); // Unique boundary marker

$message .= "--" . md5(uniqid()) . "\r\n";
$message .= "Content-Type: text/plain; charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $plain_text_content . "\r\n";

$message .= "--" . md5(uniqid()) . "\r\n";
$message .= "Content-Type: text/html; charset=utf-8\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$message .= $html_content . "\r\n";

$message .= "--" . md5(uniqid()) . "--\r\n";

// This final concatenated string is what gets sent via SMTP.

Step 2: Sending via an SMTP Library

Once the MIME structure is built, you use a robust SMTP library (like PHPMailer in PHP) to connect to the mail server and transmit this correctly formatted message. This process abstracts away the complexities of raw socket communication and ensures that headers and boundaries are set perfectly for delivery.

For modern web applications, developers often leverage frameworks like Laravel, which provides excellent tools for handling email sending through its Mail facade, abstracting away these low-level MIME details. For instance, when utilizing services built around powerful backend logic, the focus shifts from manually crafting raw message strings to ensuring your data is correctly templated and formatted before transmission, much like structuring complex data relationships in a Laravel application.

Conclusion

Sending HTML emails effectively requires moving beyond simple intent parameters and embracing the standards of MIME messaging. Whether you are building a mobile app or a web service, reliability comes from adhering to established protocols. By constructing a proper multipart/alternative structure on the server side and utilizing dedicated SMTP libraries, you ensure that your rich HTML content is delivered exactly as intended across all major email clients. Focus on robust backend construction rather than brittle platform-specific shortcuts.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.