Skip to content
Advertisement

Move email to folder if more than one partial strings found in email subject

  1. I have a list containing Folder Names at the Outlook level [‘fried chicken’; “boiled pasta”; “baked potatoes”], folders exist and are empty.
    The elements are strings that are combinations of two words example “fried chicken”.

  2. I search the Inbox and if the message.subject matches one of the elements in the list move that email to the folder.
    Example: Email subject is “How fried is the chicken” or “I like my potatoes to be baked”. It is not string in string exactly so my message.Move call doesn’t find anything relevant.

Below is the “code”.

alerts_folder = root_folder.Folders[folder_name_selected]
messages = alerts_folder.Items
z=messages.Count
message = messages.GetLast()
for message in list(messages):
    name = str(message.subject).lower()
    
`   #message = messages.GetPrevious()
    for y in top5:
        if all(x in name for x in y.split()):
            #print(str(name))
            #print("Email " + name + " goes into folder " + alerts_folder[y])
            name_of_selected_folder = str(y)
            final_destination = alerts_folder.Folders[name_of_selected_folder]
            #print("Email " + name + " goes into folder " + name_of_selected_folder)
            message.Move(final_destination)
            #message.Move(name_of_selected_folder)

Advertisement

Answer

Try:

for message in messages:
    name = str(message.subject)
    message = messages.GetPrevious()
    for y in list:
        if any(x in name for x in y.split()):
            print(str(name))
            #print("Email " + name + " goes into folder " + alerts_folder[list])
            message.Move(alerts_folder.Folders[list])

The code above firstly iterates over list and then splits every element in a list (as each element is a string of two words).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement