SoFunction
Updated on 2025-04-05

Python+imbox library implements email reading, deletion and attachment download

Get the qq email authorization code referencePython+smtplib library implements email sending function

() Parameters:

    inbox_messages = (unread=True)  # Unread email    inbox_messages = (unread=False)  # Read email    inbox_messages = (flagged=True)  # Marked emails    
    inbox_messages = (date__lt=datetime(2025, 2, 7))  # Get emails from a specified time, data from a certain day ago    inbox_messages = (date__on=datetime(2025, 2, 7))  # Get emails for a specified time, data for a certain day    inbox_messages = (date__gt=datetime(2025, 2, 7))  # Get emails at the specified time, data from a certain day later

Complete code

from datetime import datetime
from imbox import Imbox
import html
 
# Login credentialsusername = "2xx85555ss@"
password = "xxxxxxx"
 
with Imbox('', username, password, ssl=True) as imbox:
    inbox_messages = ()  # Get all emails    for uid, message in inbox_messages:
        subject =   # Email Subject        from_ = message.sent_from  # Sender        to_ = message.sent_to  # recipient        date =   # Send date 
        # print(['plain']) # Email text format body        # Process the email text        body = ''
        if 'plain' in :
            # Extract the content of the text (first element in the list)            if ['plain']:
                body = ['plain'][0]
                # Decode Unicode encoding                body = ().decode('unicode_escape')
                # Decode HTML entities                body = (body)
        else:
            print("No plain text body found.")
 
        html_body = ['html']  # html format text 
        # print() # attachment information        attachment_names = []
        for attachment in :
            file_name = ('filename')
            attachment_names.append(file_name)
 
        print(
            f'theme:{subject}\nFrom:{from_}\nrecipient:{to_}\ntime:{date}\nText content:{body}\nHTMLcontent:{html_body}\nappendix:{attachment_names}\n')

print(['plain']) The output is a Unicode-encoded string list, rather than a directly parsed text. This is because the message body content returned by imbox may be stored as a list and may contain HTML entities or Unicode encodings.

Solution

Extract strings from list: ['plain'] returns a list, you need to extract strings from list.

Decode Unicode encoding: Use Python's built-in features such as .encode().decode('unicode_escape') to convert Unicode-encoded strings into readable text.

Handling HTML Entities: If the message body contains HTML entities (such as &nbsp; or <a> tags), you can use the html module or a third-party library (such as BeautifulSoup) to parse.

Code to solve the problem:

        body = ''
        if 'plain' in :
            # Extract the content of the text (first element in the list)            if ['plain']:
                body = ['plain'][0]
                # Decode Unicode encoding                body = ().decode('unicode_escape')
                # Decode HTML entities                body = (body)
        else:
            print("No plain text body found.")

Download attachment

        attachment_names = []
        for attachment in :
            file_name = ('filename')
            attachment_names.append(file_name)
            with open(file_name, 'wb') as f:
                (attachment['content'].getvalue())

Tag emails

Can be tagged according to the content or subject of the email

        imbox.mark_seen(uid)   # Set as read        imbox.mark_flag(uid)  # Tags

Delete email

Can be deleted according to the content or subject of the email

        if 'Reminder for rejection of message push review' in subject and '2025 17:29' in date:
            (uid)  # delete

The above is the detailed content of the Python+imbox library to realize email reading, deletion and attachment download. For more information about Python imbox email, please follow my other related articles!