EML Parser Library

3.0.0 · active · verified Thu Apr 16

eml-parser is a Python library designed for parsing EML (Email Message) files, extracting headers, body content, attachments, and other email components into a structured dictionary. It is currently at version 3.0.0 and maintains an active development pace with releases for new features, bug fixes, and Python version compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the EmlParser and parse EML content from a string. The parsed data is returned as a dictionary, allowing easy access to headers, body, and other elements. An example of how to parse from a file is also included as a comment.

from eml_parser import EmlParser

eml_content = """
From: sender@example.com
To: receiver@example.com
Subject: Test Email
Date: Thu, 1 Jan 2023 12:00:00 +0000
Content-Type: text/plain; charset="utf-8"

This is a sample email body.
"""

# Initialize the parser
parser = EmlParser()

# Parse the EML content from a string
parsed_eml = parser.parse_from_string(eml_content)

# Print some extracted data
print(f"From: {parsed_eml['header']['from']}")
print(f"Subject: {parsed_eml['header']['subject']}")
if parsed_eml['body']:
    print(f"Body content: {parsed_eml['body'][0]['content']}")

# Example for parsing from a file
# with open('path/to/your.eml', 'rb') as f:
#     file_content = f.read()
#     parsed_eml_file = parser.parse_from_string(file_content.decode('utf-8', errors='ignore'))
#     print(f"File Subject: {parsed_eml_file['header']['subject']}")

view raw JSON →