1

I'm trying to filter the MailItem I extracted from outlook using pywin32.

I can filter on the sender email address just fine but i can't seem to find the proper filter to use for recipients (the "To" field in the mail).

def parse_mail():
    global mail_MIs
    pythoncom.CoInitialize()
    outlook = Client.Dispatch("outlook.Application")

    namespace = outlook.GetNamespace('MAPI')

    inbox = namespace.Folders('bla@blabla.com').Folders('Inbox')
    # Get an instance of the 2022 folder
    year2022_folder = inbox.Folders[1].Folders

    for month_folder in year2022_folder:
        for item in month_folder.Items.Restrict("[SenderEmailAddress] = 'test1@test.com' OR [SenderEmailAddress] = 'test2@test.com'"):
            # Any operation i need to do

I tried using "[To]" as a filter; but it did not work. Also i can't seem to find any proper list on what other filters i can use with the Restrict method even in the Microsoft documentation.

0m3r
  • 12,286
  • 15
  • 35
  • 71
homunculus
  • 25
  • 3

2 Answers2

0

In general, you can filter on the To/CC/BCC properties (they correspond to the PR_DISPLAY_TO / PR_DISPLAY_CC / PR_DISPLAY_BCC MAPI properties), but To/CC/BCC may or may not include the actual email addresses - in most cases, they only include ";" separated list of display names.

On the Extended MAPI level (C++ or Delphi only), one can create a subrestriction on the message recipients (or attachments for that matter) using RES_SUBRESTRICTION / PR_MESSAGE_RECIPIENTS, but Outlook Object Model does not expose that functionality

If using Redemption is an option (I am its author), you can use RDOFolder.Items.Restrict - unlike Outlook Object Model, it does expand To/CC/BCC queries into recipient sub restrictions on PR_DISPLAY_NAME and PR_EMAIL_ADDRESS properties on each recipient (RES_SUBRESTRICTION / PR_MESSAGE_RECIPIENTS / RES_OR / PR_DISPLAY_NAME | PR_EMAIL_ADDRESS).

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Folder = Session.GetFolderFromID(Application.ActiveExplorer.CurrentFolder.EntryID)
set restrItems = Folder.Items.Restrict(" TO = 'user@domain.demo' ")

You can also specify Recipients property in the SQL query - it will be matched against recipients of all types (to/cc/bb):

set restrItems = Folder.Items.Restrict(" Recipients = 'user@domain.demo' ")
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

for to. try

urn:schemas:httpmail:to Or urn:schemas:mailheader:to

see example https://stackoverflow.com/a/61091622/4539709 and helpful links

list of filters you can use


urn: Schemas: mailheader: approved
urn: Schemas: httpmail: attachmentfilename
urn: Schemas: mailheader: BCC
urn: Schemas: httpmail: BCC
urn: Schemas: httpmail: CC
urn: Schemas: mailheader: CC
urn: Schemas: mailheader: Comment
urn: Schemas: mailheader: content -base
urn: Schemas: mailheader: content -Class
urn: Schemas: mailheader: content -Description
urn: Schemas: mailheader: content -disposition
urn: schemas:httpmail:content-disposition-type
urn: Schemas: mailheader: content -ID
urn: Schemas: mailheader: content -Language
urn: Schemas: mailheader: content -Location
urn: schemas:httpmail:content-media-type
urn: Schemas: mailheader: content -transfer - Encoding
urn: schemas:mailheader:content-type
urn: Schemas: mailheader: Control
urn: Schemas: httpmail: Date
urn: Schemas: mailheader: Date
urn: Schemas: httpmail: DateReceived
urn: Schemas: httpmail: displaycc
urn: Schemas: httpmail: displayto
urn: Schemas: mailheader: disposition
urn:schemas:mailheader:disposition-notification-to
urn: Schemas: mailheader: distribution
urn: Schemas: mailheader: expires
urn: Schemas: mailheader: expiry -Date
urn: Schemas: httpmail: flagcompleted
urn:schemas:mailheader:followup-to
urn: Schemas: httpmail: From
urn: Schemas: mailheader: From
urn: Schemas: httpmail: fromemail
urn: Schemas: httpmail: fromname
urn: Schemas: httpmail: HasAttachment
urn: Schemas: httpmail: htmldescription
urn: Schemas: httpmail: Importance
urn: Schemas: mailheader: Importance
urn:schemas:mailheader:in-reply-to
urn: Schemas: mailheader: Keywords
urn: Schemas: mailheader: Lines
urn: Schemas: mailheader: Message -ID
urn: Schemas: httpmail: messageflag
urn: Schemas: mailheader: mime -Version
urn: Schemas: mailheader: Newsgroups
urn: Schemas: httpmail: normalizedsubject
urn: Schemas: mailheader: Organization
urn: Schemas: mailheader: original -Recipient
urn: Schemas: mailheader: Path
urn: Schemas: mailheader: posting -Version
urn: Schemas: httpmail: Priority
urn: Schemas: mailheader: Priority
urn: Schemas: mailheader: Received
urn: Schemas: mailheader: References
urn: Schemas: mailheader: relay -Version
urn: Schemas: httpmail: Reply -By
urn: Schemas: mailheader: Reply -By
urn:schemas:httpmail:reply-to
urn:schemas:mailheader:reply-to
urn:schemas:mailheader:return-path
urn:schemas:mailheader:return-receipt-to
urn: Schemas: httpmail: savedestination
urn: Schemas: httpmail: saveinsent
urn: Schemas: mailheader: Sender
urn: Schemas: httpmail: Sender
urn: Schemas: httpmail: SenderEMail
urn: Schemas: httpmail: SenderName
urn: Schemas: mailheader: Sensitivity
urn: Schemas: httpmail: Subject
urn: Schemas: mailheader: Subject
urn: Schemas: httpmail: Submitted
urn: Schemas: mailheader: summary
urn: Schemas: httpmail: textdescription
urn: Schemas: mailheader: thread -Index
urn: Schemas: mailheader: thread -topic
urn: Schemas: httpmail: thread -topic
urn:schemas:httpmail:to
urn:schemas:mailheader:to
urn: Schemas: mailheader: x -Mailer
urn: Schemas: mailheader: x -Message - completed
urn: Schemas: mailheader: x -Message - flag
urn: Schemas: mailheader: x -unsent
urn: Schemas: mailheader: xref
0m3r
  • 12,286
  • 15
  • 35
  • 71