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

3 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
0
Tony Scholz Replied
Employee Post
Hello, 

You can also use the built-in API. 

The ListMailFolders call will get the number of messages per folder, you will need to loop over the domains and user or just what you are looking for. 

Example
def print_folder_info(folder, level=0):
    indent = "  " * level
    print(f"{indent}Folder: {folder['displayPath']}, totalMessages: {folder['totalMessages']}, size: {folder['size'] / 1024**2:.2f} MB")
    for subfolder in folder.get('subFolders', []):
        print_folder_info(subfolder, level + 1)

# ListMailFolders
_url = f"{sup_ascholz.uri}api/v1/folders/list-email-folders"
response = requests.get(url=_url, headers=sup_ascholz.HeaderImpersonate)

if response.status_code == 200:
    for folder in response.json().get('folderList', [])[:10]:
        print_folder_info(folder)


An example of the Output 

Folder: __ARCHIVE, totalMessages: 1457, size: 12.16 MB
Folder: _CAL_Invites, totalMessages: 0, size: 0.00 MB
  Folder: _CAL_Invites/Teams, totalMessages: 1, size: 0.01 MB
  Folder: _CAL_Invites/Zoom, totalMessages: 4, size: 0.08 MB
Folder: _DISL_SPACE_THRESHOLD_, totalMessages: 2241, size: 19.24 MB
Folder: /new_folder, totalMessages: 0, size: 0.00 MB
  Folder: /new_folder/sent, totalMessages: 0, size: 0.00 MB
Folder: 075-2E3528C9-0014, totalMessages: 0, size: 0.00 MB
  Folder: 075-2E3528C9-0014/7252024 133 PM, totalMessages: 23, size: 1.19 MB
  Folder: 075-2E3528C9-0014/mail12.newtechweb.com, totalMessages: 6, size: 1.18 MB
    Folder: 075-2E3528C9-0014/mail12.newtechweb.com/Sub-001, totalMessages: 0, size: 0.00 MB
      Folder: 075-2E3528C9-0014/mail12.newtechweb.com/Sub-001/Sub-002, totalMessages: 0, size: 0.00 MB
Folder: 07D-2EF4A1D3-0003, totalMessages: 0, size: 0.00 MB
Folder: 09F-2F2E7A3F-0002, totalMessages: 5, size: 0.47 MB
Folder: 16B-2F19565F-002A, totalMessages: 2, size: 0.05 MB
Folder: 1BB-2E3BC535-001E, totalMessages: 56, size: 26.01 MB
Folder: 1BE-2E6A1C33-0027, totalMessages: 0, size: 0.00 MB
  Folder: 1BE-2E6A1C33-0027/GRP, totalMessages: 837, size: 45.39 MB

Tony Scholz System/Network Administrator SmarterTools Inc. www.smartertools.com

Reply to Thread

Enter the verification text