Python Forum
Trying out the parsing/reading of emails from my outlook
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying out the parsing/reading of emails from my outlook
#1
I'm dabbling in reading emails from python and have the following code just to get my feet wet in doing so, but wanted to see how i can look for certain key words in the body of emails..

import win32com.client

# import os
# from datetime import datetime, timedelta

outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")

# for account in mapi.Accounts:
# 	print(account.DeliveryStore.DisplayName)
    
inbox = mapi.GetDefaultFolder(6)
inbox = mapi.GetDefaultFolder(6).Folders["Emails 2021"]

messages = inbox.Items

messages = messages.Restrict("[Subject] = 'Software - SAS'")
for message in messages:
    print(message.body)
The displayname i have commented out works and displays my email address/account. And when i run i do get the email body for the 1 email i have in that folder I'm testing with. So i was looking at some other sites and examples and couldnt really find a good working sample on what functions that could be called to search within the body of any email for keywords..

Outlook has great message filters and i use those for placing the emails into their own folders.. But what im testing and playing around with is being able to look thru those emails(eventually) for important emails that may contain certain key words within the body.. Starting off the new year we will be starting off with new projects, so someone could send an email with a subject that has nothing to do with the project, but in the body, there is mention of the project.. so for my first attempt, id like to see if i can run a script that will look in certain folders(in this case that would be "Emails 2021")

Eventually if this is possible, i would like to have a list of keywords/project names or project words that i can search for..

I tried using the .contains but got an error on this code:
for message in messages:
    if message.contains("MFP"):
        print(message.body)
    print(message.body)
So im looking for what the correct syntax for searching within the body for certain words/strings is?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Outlook Inbox for set of values cubangt 1 1,063 Jun-28-2023, 09:29 PM
Last Post: cubangt
  Save image from outlook email cubangt 1 694 Jun-07-2023, 06:52 PM
Last Post: cubangt
  Read All Emails from Outlook and add the word counts to a DataFrame sanaman_2000 0 1,854 Sep-15-2022, 07:32 AM
Last Post: sanaman_2000
  Sending Emails on Autopilot Gyga_Hawk 3 1,683 Mar-15-2022, 08:20 AM
Last Post: Larz60+
  Mark outlook emails as read using Python! shane88 2 6,543 Feb-24-2022, 11:19 PM
Last Post: Pedroski55
  Need Outlook send email code using python srikanthpython 3 8,215 Feb-28-2021, 01:53 PM
Last Post: asyswow64
  Outlook Emails & HTML Table in Message Body JoeDainton123 1 11,377 Sep-02-2020, 05:15 AM
Last Post: buran
  reading shared outlook emails zarize 0 2,448 Mar-03-2020, 01:47 PM
Last Post: zarize
  Python Library for Reading POP Emails? bmccollum 1 3,627 Jan-06-2020, 06:37 PM
Last Post: micseydel
  Read in trades from emails semantina 2 2,100 Nov-06-2019, 06:12 PM
Last Post: semantina

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020