Registry / communication / mail-parser

mail-parser

JSON →
library4.6.4pypypi✓ verified 24d ago

mail-parser is a production-grade, RFC-compliant email parsing library that enhances the Python standard library's email module. It extracts all details into a comprehensive object, including headers, plain text and HTML bodies, attachments, and routing information, with a strong focus on security analysis and defect detection. It provides access to parsed elements as Python objects, raw strings, and JSON. The library is actively maintained.

pip install mail-parser
INSTALL
IMPORT
SIG · MAIL-PARSER
M
mail-parser
communicationpythonv4.6.4
Install
1.5s avg
Import
105ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.6.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.108s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.102s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mailparser
import mailparser
Main library import for accessing parsing functions.
parse_from_string
mail = mailparser.parse_from_string(raw_email_string)
One of several factory methods to parse emails from different sources (string, bytes, file, etc.).

Parses a raw email string and extracts basic headers and the plain text body. Demonstrates accessing structured email components like sender, subject, and body content.

import mailparser raw_email = ( "From: Sender Name <sender@example.com>\n" "To: Recipient Name <recipient@example.com>\n" "Subject: Test Email from mail-parser\n" "Content-Type: text/plain; charset=\"utf-8\"\n" "\n" "Hello, this is the plain text body of the email.\n" "It was parsed using the mail-parser library." ) mail = mailparser.parse_from_string(raw_email) print(f"From: {mail.from_}") print(f"Subject: {mail.subject}") if mail.text_plain: print(f"Body: {mail.text_plain[0]}") # Example of accessing the 'to' address list for recipient in mail.to: print(f"To Name: {recipient[0]}, Address: {recipient[1]}")
Debug
Known issues
breakingSome parsing behavior for complex or malformed emails may have changed between versions 3.x and 4.x. A GitHub issue indicates a regression where emails parse successfully in 3.15.0 but fail in 4.0.0. Users upgrading from 3.x to 4.x should thoroughly test their email parsing routines, especially for edge cases.
fix
Review existing parsing logic and test against the new version. Consult GitHub issues or the project's changelog for specific changes if parsing failures occur. Consider reporting new regressions if unaddressed.
affects: >=4.0.0
gotchaParsing Microsoft Outlook .msg files requires an external system dependency on Debian-based systems. You need to install `libemail-outlook-message-perl` for this functionality.
fix
On Debian/Ubuntu: `sudo apt-get install libemail-outlook-message-perl`. For other operating systems, consult the project's documentation for equivalent dependencies or alternatives.
affects: All
gotchaThe `mail-parser` library can be confused with other similarly named Python packages (e.g., `fast_mail_parser`, `mailparse`, `msg-parser`) or web services (e.g., `Mailparser.io`). Ensure you are importing and using the correct library from the `SpamScope` organization.
fix
Always verify the PyPI project page (https://pypi.org/project/mail-parser/) and the GitHub repository (https://github.com/SpamScope/mail-parser) to confirm you have the correct package. Use `pip install mail-parser` for installation.
affects: All
gotchaWhen accessing the 'From' header, use `mail.from_` (with an underscore) instead of `mail.from` because `from` is a reserved keyword in Python. Attempting to use `mail.from` will result in a `SyntaxError`.
fix
Always append an underscore when referring to the 'From' header attribute, e.g., `parsed_mail.from_`.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'mailparser'
The `mail-parser` library has not been installed in the current Python environment, or the import statement uses an incorrect module name.
fix
Install the library using pip: `pip install mail-parser`. Ensure your import statement is `import mailparser` (lowercase, no hyphen).
KeyError: 'content-transfer-encoding'
This error occurs when `mail-parser` attempts to access the 'content-transfer-encoding' header, but the email being parsed does not contain this header, which can happen with malformed or non-standard emails.
fix
Implement error handling or checks for the presence of the header before attempting to access it, or ensure the email source is well-formed. For robust parsing, `mail-parser` should ideally handle such missing headers gracefully, but a workaround might involve checking `mail.headers.get('content-transfer-encoding')` to avoid direct KeyError.
TypeError in ported_string when parsing headers as email.header.Header objects
This specific `TypeError` indicates an incompatibility or an unexpected format when `mail-parser` processes email headers that are represented internally as `email.header.Header` objects, possibly due to a bug or an edge case in how the library interacts with Python's standard `email` module.
fix
Check for updates to the `mail-parser` library (`pip install --upgrade mail-parser`) as this might be a known bug that has been patched. If the issue persists, report it to the library's GitHub repository with a minimal reproducible example.
UnicodeEncodeError: 'ascii' codec can't encode character '\xe0' in position X: ordinal not in range(128)
This error typically arises when attempting to process or encode email content (like subject, body, or headers) that contains non-ASCII characters using an 'ascii' codec, which cannot handle such characters.
fix
Ensure that email content is consistently handled as bytes with an appropriate encoding (e.g., UTF-8) when parsing or reconstructing. When working with raw email data, parse from bytes using `mailparser.parse_from_bytes(email_bytes)` and handle decoded strings with explicit UTF-8 encoding/decoding where necessary.
Upgrade
Version history
4.6.4latest on PyPI · released Aug 21, 2026
Audit
Dependencies
pythonrequiredRuntime environment
Agent activity
28 hits · last 30 days
node
22
OpenAI (training)
1
Resources
mail-parser — pip install mail-parser · libregistry