flufl.bounce: Email Bounce Detectors

4.0 · active · verified Thu Apr 16

flufl.bounce is a Python library designed for robust detection and parsing of email bounce messages. It helps identify undeliverable emails, extract recipient information, and understand the reason for delivery failures. The current major version is 4.0, released in August 2023, and it receives updates as needed, typically for Python compatibility or bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a raw bounce email string into an `email.message.Message` object, then use `flufl.bounce.Bounce` to detect if it's a bounce and extract key information like bounce type, failed recipients, and diagnostic codes. Remember to always pass an `email.message.Message` object to the `Bounce` constructor.

from email import message_from_string
from flufl.bounce import Bounce

# Simulate a bounce email content
bounce_email_content = """
From: MAILER-DAEMON@example.com
To: original_sender@example.com
Subject: Undelivered Mail Returned to Sender

This is the mail system at host example.com.

I'm sorry to have to inform you that your message could not be delivered to one or more recipients.
It's attached below.

<nonexistent@example.com>: host mail.example.com[192.0.2.1] said: 550 5.1.1
    <nonexistent@example.com>: Recipient address rejected: User unknown
"""

# Parse the raw email string into an email.message.Message object
msg = message_from_string(bounce_email_content)

# Create a Bounce object from the parsed message
bounce = Bounce(msg)

print(f"Is this a bounce message? {bounce.is_bounce}")
if bounce.is_bounce:
    print(f"Bounce type: {bounce.type}")
    print(f"Failed recipients: {bounce.recipients}")
    print(f"Diagnostic code: {bounce.diagnostic_code}")
else:
    print("Not recognized as a bounce by flufl.bounce.")

view raw JSON →