4
Rebuild Folders based on Physical Directories
Idea shared by Shaun Peet - 9/4/2021 at 5:24 PM
Under Consideration
Far too many times we have some settings.json or folders.json file issues and while there's sometimes relatively easy fixes for those, quite often we're left with having a restored mailbox that no longer has the user's custom folders.  So to fix this we have to:

Go onto the server and navigate to the "Mail" folder for the User to see what physical directories exist there (other than the standard ones)
Then, *for each* physical folder that exists on the server, use the "Rebuild Folder" button within the Admin UI by specifying the email address and the name of the folder.

In some cases, we'll have a domain with 20+ users who have 20+ custom folders, so we'll be hitting the Rebuild Folder button 400 times.  Not only is it time consuming, that's incredibly prone to errors (especially when trying to do it quickly)

What would be nice is if there was a version of the Rebuild Folder window that had an "auto-detect based on physical folders" option that looped through all the directories for all folders, including the standard ones, and made sure that every folder on the server was visible within the Webmail.  If for whatever reason that creates extra folders in the Webmail, then the user at that point can decide if they want to delete them along with any messages within them.

That option gets you 3 gold stars.

For 4 gold stars, make that a domain-level "Rebuild All Folders Based on Physical Directories For All Users" button.  In our case it seems like whenever folders go missing for one user within a domain, all the users on the same domain have the same issue.  So for us it's pretty common to have to do this for all users in a domain, so a button that looped through them all would be great.

For 5 gold stars, make it a server-wide button to repair all users of all domains.  We had a pretty big server issue last week where somewhere between 10 and ??? (reports are still coming in) of our 500+ domains on the server had various forms of corruption.  Having a server-wide "repair all" button would allow us to be proactive with our users rather than reactive.

5 Replies

Reply to Thread
1
+1
Gabriele Maoret - Head of SysAdmins at SERSIS Currently manages 6 SmarterMail installations (1 in the cloud for SERSIS which provides services to a few hundred third-party email domains + 5 on-premise for customers who prefer to have their mail server in-house)
0
Zach Sylvester Replied
Employee Post
Hey Shaun, 

Thank you for posting on the community. 

You could use the API to do this. 
import requests
global auth
import time
class SMAPI:
    
    def __init__(self, username, password, url):
        self.username = username
        self.password = password
        self.url = url
    

    def auth(self):
        global auth
        username = self.username
        password = self.password
        url = self.url
        authurl = url + "/api/v1/auth/authenticate-user"
        myobj = {'username': username, 'password':password}
        x = requests.post(authurl, data = myobj)
        auth = str(x.text)
        auth = auth.split(",")
        auth = auth[11]
        auth = auth.split(":")
        auth = auth[1]
        auth = auth.replace("""\"""", "")
        


    def RestoreFolders(self,folder,email,recursive):
    
        global auth
        url = self.url + "/api/v1/settings/sysadmin/restore-folders"
        

        myobjs = {"restorations":[{'folder':folder,'email':email,'recursive':recursive}]}

        header = {'Authorization' : 'Bearer ' + auth}
        x = requests.post(url, json = myobjs, headers = header)
        x.encoding = x.apparent_encoding
        return(x.text)


me = smapi.SMAPI("zjs81@example.com","admin1","https://domain.com";)


me.auth()

me.RestoreFolders('C:\smartermail\Domains\example.com\user\mail\folder', 'zjs81@example.com', 'true') 
This is an example of how I would do it. You can then just use another script to get all the paths of users per domain or for the whole server.
I hope this helped. 
I can still submit this as a feature request if you wish please let me know. 
Best Regards,
Zach Sylvester System/Network Administrator SmarterTools Inc. (877) 357-6278 www.smartertools.com
0
Kyle Kerst Replied
Employee Post
This functionality has been requested and is being evaluated by our Product Management team. I've associated your community post with that request and have noted this post as Under Consideration. Unfortunately, I can't provide any ETA on when this evaluation will finish, nor when we might see the proposed functionality, and I recommend keeping an eye on our release notes for updates on this front.

As to root causes, the vast majority of the time we see these problems introduced by: 

1. Disk or disk controller failure. 
2. Client issues (usually seen on older email clients.)
3. Sudden restarts of the server that are unexpected. 
4. Corruption or locking of the JSON files by an antivirus or backup solution.

In the meantime I recommend leveraging the API for this as Zach noted, as that should allow you to correct these problems programmatically. Thanks for your time and suggestions on this Shaun. Have a great day!
Kyle Kerst System/Network Administrator SmarterTools Inc. (877) 357-6278 www.smartertools.com
0
Hi Zach & Kyle,

If I'm understanding the API example provided, I don't see anywhere that the API is "looking up" what physical folders exist within a mailbox in order to trigger a rebuild on those folder(s)?

Shaun

0
Zach Sylvester Replied
Employee Post
Hello Shaun, 

That is correct sadly there is no way currently using the API to determine what the folder paths are. 
But you could use a script like the following to find the paths and add it for you. 

def fixfoldersWeb():
    import smapi
    print("Enter SmarterMail admin api creds example user:admin Password admin url http://sup-zsylvester.st.local";)
    username_sm = input("input Username ")
    password_sm = input("input password ")
    url_sm = input("input url ")
    
    userpath = ''
    u_i = input("Enter Path To Domains Folder ")
    domains = [name for name in os.listdir(u_i) if os.path.isdir(os.path.join(u_i, name))]
    for i in domains:
        usersdir = u_i + '''\\''' + str(i)+ '''\\Users'''
        os.chdir(usersdir)
        users = [name for name in os.listdir(usersdir) if os.path.isdir(os.path.join(usersdir, name))]
        for num in users:
            userpath = usersdir+'''\\'''+num+'''\\Mail'''
            #print(userpath)
            try:
                os.chdir(userpath)
                files = [name for name in os.listdir(userpath) if os.path.isdir(os.path.join(userpath, name))]
                for walle in files:
                    if 'Deleted Items' == str(walle):
                        pass
                    elif 'Drafts' == str(walle):
                        pass
                        
                    elif 'Inbox' == str(walle):
                        pass
                        
                    elif 'Junk E-Mail' == str(walle):
                        pass
                        
                    elif 'Sent Items' == str(walle):
                        pass
                        
                   
                    else:
                        print("add to user "+num+"@"+i + " " + " Folder name " + " " + walle)
                        me = smapi.SMAPI(username_sm,password_sm,url_sm)
                        me.auth()
                        print(me.RestoreFolders(walle,str(num+"@"+i),"true"))
    
            except:
                pass
                

    
This script looks in the domains dir then goes through all the domains and users and gets the path then uses the API rebuild folder function to rebuild all the folders. 

I hope this helps. 

Best Regards, 
Zach Sylvester System/Network Administrator SmarterTools Inc. (877) 357-6278 www.smartertools.com

Reply to Thread