[Python] Problem identifying a specific email sender

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I am facing a problem at work where I need to create the following script/automation using Python:

    • Read the subject and content of an email in Outlook to check if a request has been confirmed. Based on this information, act as follows:
    • If the request has not been confirmed, reply to the email requesting a response.
    • If the request has been confirmed, send an email confirming that the operation was successfully completed.

    However, I'm stuck on a problem trying to identify the email sender. I'm encountering the following error: "/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=D5C4A87552E64C4F879F178A9710A189- sender's name"

    The script itself returns normal "@outlook.com" e-mails, but not with specific domains on it....

    I've tried many alternatives to solve this problem, but it seems impossible... I'm a begginer at coding, so sorry for the incovenience (Btw, I'm running this code in Jupyter / Anaconda3)

    Here is my code showing my progress so far:

    import win32com.client
    from datetime import datetime, timedelta

    # Connect to Outlook
    outlook = win32com.client.Dispatch("Outlook.Application")
    namespace = outlook.GetNamespace('MAPI')

    # Access the Inbox
    inbox = namespace.GetDefaultFolder(6)

    # Function to search for emails containing any term from the list in the body or subject
    def mail_body_search(terms, folder):
    term_counts = {term: 0 for term in terms} # Dictionary to store counts of each term
    relevant_messages = [] # List to store relevant emails
    senders = set() # Set to store unique sender emails

    # Filter emails that contain any term in the body or subject
    for message in folder.Items: # Iterate over items in the folder
    for term in terms: # Check each term in the list
    if term.lower() in message.Body.lower() or term.lower() in message.Subject.lower():
    relevant_messages.append((message, message.Parent.Name)) # Add relevant email
    term_counts[term] += 1 # Increment count for the found term
    senders.add(message.SenderEmailAddress) # Add sender's email to the set
    break # If the term is found, no need to check other terms for this email

    return relevant_messages, term_counts, senders # Return the list of found emails, term counts, and unique senders

    # List of terms to search
    search_terms = ['Teste', 'Testing with python']

    # Call the function to search for emails with any of the terms in the Inbox
    results, term_counts, senders = mail_body_search(search_terms, inbox)

    # Display the total number of emails found
    print(f'A total of {len(results)} emails were found.')

    # Display the number of emails found for each term
    for term, count in term_counts.items():
    print(f'{count} emails were found for "{term}".')

    # Display the unique sender emails
    print('Unique sender emails:')
    for sender in senders:
    print(sender)

    Continue reading...

Compartilhe esta Página