Mailbits

0.2.3 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `mailbits` to parse and format email address strings, and how to convert an `EmailMessage` object from the standard library into a structured Python dictionary for easier manipulation or serialization.

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}")

view raw JSON →