Mailbits
Mailbits is a Python library that provides a small assortment of utility functions designed to work with Python's standard library `email` package, specifically its `Message`, `EmailMessage`, `Address`, and `Group` types. It offers functionalities like parsing and reassembling `Content-Type` strings, converting older `Message` objects to the newer `EmailMessage` format, transforming `Message` and `EmailMessage` instances into structured dictionaries, and robust parsing and formatting of email addresses. The current version is 0.2.3, and it appears to have an as-needed or sporadic release cadence, typical for focused utility libraries.
Common errors
-
ModuleNotFoundError: No module named 'mailbits.converters'
cause Attempting to import utilities from a submodule (e.g., `converters`) that are directly exposed at the top level of the `mailbits` package.fixImport directly from `mailbits`: `from mailbits import message2email, email2dict`. -
TypeError: argument must be Message or EmailMessage, not str
cause Passing a plain string or an incorrect object type to functions like `message2email()` or `email2dict()`, which expect `email.message.Message` or `email.message.EmailMessage` objects.fixEnsure you construct a proper `email.message.EmailMessage` (or `Message`) object from your string content before passing it to `mailbits` utility functions. For example: `from email.message import EmailMessage; msg = EmailMessage(); msg.set_content(your_string_body)`. -
AttributeError: 'Address' object has no attribute 'name' (or 'mailbox', 'host')
cause Attempting to access attributes like `name`, `mailbox`, or `host` directly on `email.headerregistry.Address` objects returned by `parse_addresses()`. `Address` objects use `display_name` and `addr_spec` for name and full address, respectively.fixUse `address_obj.display_name` for the display name (or an empty string if none) and `address_obj.addr_spec` for the actual email address (e.g., 'user@domain.com').
Warnings
- gotcha When using `message2email()`, be aware that it specifically converts instances of the older `email.message.Message` class to the newer `email.message.EmailMessage`. If you're already working with `EmailMessage` objects, this function will return them unchanged. Ensure you understand the distinction between the two classes in Python's `email` module, especially when integrating with older codebases or third-party libraries that might still return `Message` objects.
- gotcha The `parse_addresses()` and `format_addresses()` functions are powerful, but they rely on RFC compliance. Malformed or highly unusual address strings might not parse as expected. While `mailbits` aims to be robust, edge cases exist.
- breaking As a 0.x.x version library, `mailbits` does not strictly adhere to semantic versioning. Minor versions (e.g., 0.2.x to 0.3.x) *could* introduce breaking API changes without a major version increment. Always review release notes when upgrading.
Install
-
pip install mailbits
Imports
- parse_addresses
from mailbits import parse_addresses
- format_addresses
from mailbits import format_addresses
- message2email
from mailbits.converters import message2email
from mailbits import message2email
- email2dict
from mailbits import email2dict
- ContentType
from mailbits import ContentType
Quickstart
from email.message import EmailMessage
from mailbits import parse_addresses, format_addresses, email2dict
# Example 1: Parsing and formatting addresses
address_string = '"John Doe" <john.doe@example.com>, "Jane Smith" <jane.smith@test.org>'
parsed_addresses = parse_addresses(address_string)
print(f"Parsed Addresses: {parsed_addresses}")
formatted_addresses = format_addresses(parsed_addresses)
print(f"Formatted Addresses: {formatted_addresses}")
# Example 2: Converting an EmailMessage to a dictionary
msg = EmailMessage()
msg['Subject'] = 'Hello from Mailbits'
msg['From'] = 'sender@example.com'
msg['To'] = 'recipient@example.com'
msg.set_content('This is a test email body.')
msg_dict = email2dict(msg)
print(f"\nEmail as Dictionary: {msg_dict}")