1
How to get number of messages per folder per user/ domain
Question asked by Vikram Agarwal - Yesterday at 5:33 AM
Answered
How to get report of number of messages in each folder of the user/ domain indexed
Please help

2 Replies

Reply to Thread
0
Zach Sylvester Replied
Employee Post Marked As Answer

Hello,

Thank you for reaching out with your question! SmarterMail doesn’t provide a message count per folder for users; instead, it shows the amount of space used (in MB/KB) per folder.

To get the count of messages per folder, you would need to use a Python script like the one below.

I hope this helps!


import imaplib
import ssl

IMAP_SERVER = 'imap.example.com'
EMAIL = 'user@example.com'
PASSWORD = 'yourpassword'

def list_folders_and_message_counts():
    context = ssl._create_unverified_context()
    with imaplib.IMAP4_SSL(IMAP_SERVER, ssl_context=context) as mail:
        mail.login(EMAIL, PASSWORD)
        status, folders = mail.list()
        if status != 'OK':
            print("Failed to retrieve folder list.")
            return

        for folder in folders:
            parts = folder.decode().split(' "/" ')
            if len(parts) != 2:
                continue
            folder_name = parts[1].strip('"')
            try:
                result, _ = mail.select(f'"{folder_name}"', readonly=True)
                if result != 'OK':
                    print(f'Skipped {folder_name}: cannot be selected.')
                    continue

                status, message_ids = mail.search(None, 'ALL')
                if status == 'OK' and message_ids[0]:
                    count = len(message_ids[0].split())
                    print(f'{folder_name}: {count} messages')
                else:
                    print(f'{folder_name}: 0 messages')
            except Exception as e:
                print(f'Error accessing {folder_name}: {e}')

if __name__ == '__main__':
    list_folders_and_message_counts()

Kind Regards, 
Zach Sylvester Software Developer SmarterTools Inc. www.smartertools.com
0
Vikram Agarwal Replied
I suggest to implement this feature as it will be helpful in auditing and complaince in certain cases

Regards

Reply to Thread

Enter the verification text