2026-07-15

Mailx send html message

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Mailx send html message

Sending Rich HTML Emails with Mailx: Mastering MIME Headers

As developers, moving beyond simple plain-text communication to sending rich, formatted HTML emails often introduces complexity. When working with command-line tools like mailx or standard mail utilities, the difficulty usually lies not in the sending mechanism itself, but in correctly structuring the Message-Format Information (MIME) headers required by email protocols.

I’ve seen many developers run into this exact issue when trying to pipe HTML files directly: the system defaults the message type to text/plain, regardless of the content you attach, which breaks the rendering in webmail clients. Let's dive into why this happens and how we can reliably construct true HTML emails using command-line tools.

The Challenge with Plain Text Defaults

When you execute a basic command like:

mailx -s "Subject" user@gmail.com < email.html

The mail transfer agent (MTA) processes the file content (email.html) and sets the default Content-Type header to text/plain. Even if you try to add custom headers using options like -a, these might only modify attachment details rather than overriding the core message type, leading to a poorly formatted email where the HTML is treated as raw text.

Your attempt to inject headers via command substitution:

mailx -s "$(echo -e "This is the subject\nContent-Type: text/html")" user@gmail.com < email.html

was a step in the right direction, but it highlights that we need precise control over the entire message envelope, not just the content. We must explicitly define the structure of the MIME message.

The Developer Solution: Explicit MIME Construction

The key to sending HTML correctly via command line is understanding that an email is fundamentally a set of structured text blocks (MIME parts) separated by specific boundaries. To send HTML, you need two distinct parts: the standard text metadata and the actual HTML payload, clearly demarcated by headers.

Instead of relying on file input alone, we should construct the entire message structure as a single string that mailx can ingest. This gives us absolute control over every header line.

Here is the robust method for crafting an HTML email:

Step 1: Define the Full Message Structure

We will combine the necessary headers (From, To, Subject, and crucially, the MIME type) with the actual HTML content into one command string.

#!/bin/bash

SUBJECT="My Automated HTML Report"
SENDER="sender@example.com"
RECIPIENT="recipient@example.com"
HTML_CONTENT="<h1>Hello World!</h1><p>This is the formatted message.</p>"

# Construct the full mail message body
MAIL_BODY=$(cat <<EOF
From: ${SENDER}
To: ${RECIPIENT}
Subject: ${SUBJECT}
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

<h1>${HTML_CONTENT}</h1>
EOF
)

# Pipe the constructed body directly to mailx (or use a similar tool)
echo "$MAIL_BODY" | mailx -s "$SUBJECT" "$SENDER"

Step 2: Execution and Explanation

When you execute this script, you are no longer relying on mailx to guess the content type from an attached file. Instead, you are providing a fully formed MIME message where the Content-Type: text/html header explicitly tells the receiving mail server and client exactly how to render the subsequent body data.

This approach effectively bypasses the ambiguity of file piping and enforces the correct structure required for HTML delivery. This level of meticulous structuring mirrors the rigorous attention to data schema we see in modern application development, much like ensuring consistent API responses within a framework like Laravel. Robust systems demand explicit definitions over implicit assumptions.

Conclusion

Sending complex formats like HTML via command-line utilities requires shifting the mindset from simple file input to explicit MIME construction. By manually crafting the message body with precise headers—specifically defining Content-Type: text/html and ensuring proper encoding—we gain complete control over the email's presentation. While tools like mailx are powerful, mastering the underlying protocols ensures that your communication is not just sent, but correctly interpreted by every recipient. Always prioritize explicit structure when dealing with complex data transmission.

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.