Convert HTML Email to Markdown
You need to extract the actual content from an HTML email, but the source code is a nightmare of nested tables, inline CSS, and tracking pixels.
The fastest way to strip away the formatting and get clean, readable text is to use a dedicated converter. If you just need to process a few emails, save the email as an HTML file and drop it into our free HTML to Markdown converter.
But if you are building an automated pipeline to feed emails into an LLM or a vector database, you need to understand why email HTML is so difficult to parse and how to handle it programmatically.
Why HTML Emails Are So Messy
Unlike modern webpages that use semantic tags like <article> and <nav>, HTML emails are built using techniques from the late 1990s. To ensure compatibility across dozens of different email clients (like Outlook, Gmail, and Apple Mail), developers rely on deeply nested <table> structures for layout and inline style attributes instead of external CSS.
When you try to feed this raw HTML into an AI model, you waste thousands of tokens on layout code that provides no semantic value. Converting the email to Markdown strips away the presentation layer, leaving only the headings, paragraphs, lists, and links.
Method 1: The Quick Manual Conversion
If you are archiving newsletters to Obsidian or preparing a few emails for a prompt, you don't need to write code.
- Export the Email: In your email client, look for an option to "Download message" or "Save as". Save the file as
.htmlor.eml. (If you have an.emlfile, you may need to open it in a text editor and extract the HTML part). - Convert: Go to file2markdown.ai and select the HTML converter.
- Upload: Drag and drop your HTML file.
- Copy: Copy the resulting Markdown.
This process instantly removes the layout tables and tracking pixels, giving you clean text.
Method 2: Programmatic Conversion with Python
If you are building an automated ingestion pipeline, you can use Python libraries to handle the conversion. The most common approach is to use BeautifulSoup to clean the HTML, followed by markdownify.
from bs4 import BeautifulSoup
from markdownify import markdownify as md
def email_html_to_markdown(raw_html):
# 1. Parse the HTML
soup = BeautifulSoup(raw_html, 'html.parser')
# 2. Remove tracking pixels (usually 1x1 images)
for img in soup.find_all('img'):
if img.get('width') == '1' or img.get('height') == '1':
img.decompose()
# 3. Remove style and script tags
for element in soup(["style", "script"]):
element.decompose()
# 4. Convert the cleaned HTML to Markdown
cleaned_html = str(soup)
markdown_text = md(cleaned_html, strip=['table']) # Strip layout tables
return markdown_text
# Example usage
with open('newsletter.html', 'r', encoding='utf-8') as f:
html_content = f.read()
print(email_html_to_markdown(html_content))
Notice that we explicitly tell markdownify to strip tables. Because emails use tables for layout rather than data, converting them to Markdown tables usually results in an unreadable mess.
Edge Cases to Consider
When converting HTML emails, you will encounter a few specific challenges:
- Layout Tables vs. Data Tables: As mentioned above, most tables in emails are for layout. However, if an email contains an actual data table (like a receipt or invoice), stripping all tables will destroy that data. You may need to write custom logic to detect data tables (e.g., tables with
<th>tags) and preserve them. - Tracking Links: Email links are often wrapped in redirect URLs for click tracking (e.g.,
https://click.example.com/track?url=...). The Markdown conversion will preserve these long, ugly URLs. If you want the original destination, you will need to resolve the redirects programmatically. - Base64 Images: Some emails embed images directly in the HTML using Base64 encoding. This can result in massive Markdown files if the image tags are preserved. It is usually best to strip these images entirely.
Frequently Asked Questions
Q: Can I convert an .eml file directly to Markdown?
A: An .eml file is a raw MIME message that contains headers, plain text, and HTML parts. You first need to parse the .eml file (using Python's email module, for example) to extract the HTML payload, and then convert that HTML to Markdown.
Q: Why is the Markdown output sometimes missing line breaks?
A: Email clients often use <div> or <br> tags in non-standard ways to create spacing. If the converter doesn't interpret these correctly, paragraphs might run together. Using a robust converter like the one at file2markdown.ai helps mitigate this.
Q: Is it better to use the plain text version of the email?
A: Most emails include a text/plain alternative alongside the text/html version. If the plain text version is well-formatted, you can use it directly. However, many senders neglect the plain text version, resulting in missing links or broken formatting. Converting the HTML to Markdown usually yields better results.
Need to extract clean text from messy HTML emails without writing custom parsing scripts? Try file2markdown.ai for free today. We handle the complex nested tables and inline styles automatically. If you need to process thousands of emails for your RAG pipeline, check out our Pro pricing for API access.
The File2Markdown Newsletter
Markdown tips, AI workflows, and document automation. Weekly, no spam.