Custom Mail Folder Creation - IMAP - Python

Custom Mail Folder Creation - IMAP - Python

Use Case Description

Recently I came across an automation opportunity where I had to deal with Shared Mailbox Management which involved:

  1. Reading mails
  2. Mail movement
  3. Mail folder creation (nested folders) e.g., Inbox/Subfolder1/Subfolder2

UiPath has Outlook activities that are easy to use. However, Outlook activities were not sufficient for my project as Shared Mailbox was quite heavy which caused Outlook to hang frequently.
Because of this, I decided to switch to IMAP activities instead of Outlook activities as it is not dependent on the Outlook application. However, with help of IMAP activities, I could only achieve 2 objectives out of 3 i.e., Reading Mails & Mail Movement. This left me with the main challenge to create nested folders in Shared Mailbox.
As I could not find anything related to creating mail folders using IMAP in Forum, I tried using Outlook & IMAP activities together. However, this too came up with an issue as the folder created using Outlook activities would take some time before it gets synchronized online. Hence it used to throw an error (folder does not exist) while trying to move mail to a newly created folder using IMAP.

Finally, after some research, found a python library (imaplib) that made folder creation easy using IMAP. It hardly takes a second to create a mail folder and it gets synchronized instantly.

Below is the python code, that can be used to create custom mail folders in the mailbox.

import imaplib

M = imaplib.IMAP4_SSL(“imap.example.org”,“port number in integer”)
M.login(“username”, “password”)
M.create(“New Folder Name”)
M.logout

Please note,

  1. If you are trying to access the shared mailbox, you have to update the shared mailbox name after the individual email ID (which has access to said shared mailbox) e.g., email@org.com\sharedmailbox
  2. In order to create nested folders, you have to use forward slash / to separate subfolders e.g., Inbox/SubFolder1/Subfolder2/Subfolder3
  3. This code will create folders for the entire hierarchy if it doesn’t exist already. e.g., If the mailbox has only an Inbox folder however Subfolder1, Subfolder2 & Subfolder 3 are not available, it will go ahead and create these folders in a nested way. i.e., Inbox> Subfolder1 > Subfolder2 > Subfolder3

Hope this helps the community :slight_smile:

Happy Automating!!!

AS-IS WORKFLOW, TO-BE WORKFLOW

-

Other information about the use case

Industry categories for this use case: Other Sector

Skill level required: Advanced

UiPath Products that were used: UiPath Studio

Other applications that were used: Python

Other resources: imaplib — IMAP4 protocol client — Python 3.10.7 documentation

What is the top ROI driver for this use case?: Accelerate growth and operational efficiency

3 Likes

Hi Bro, Can you share me the use case, so that I can also learn it.

Sure @vikram_Rajpurohit , I have already mentioned how it works in above use case. However if you would like to know more about it, plesae DM me on forum account.

Happy Automating :fire: