Python to retrieve a person's email and the person's manager's email

1.7k views Asked by At

I am trying to find a person's email and the person's line manager. I use code below:

import win32com.client

o = win32com.client.gencache.EnsureDispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")

adrLi = ns.AddressLists.Item("Global Address List")
contacts = adrLi.AddressEntries
numEntries = adrLi.AddressEntries.Count

nameAliasDict = {}

for i in contacts:
    name = i.Name
    email = i.Address
    manager = i.Manager
    alias = i.Address.split("=")[-1]
    nameAliasDict[alias] = name

    if "David" in name:                # any David's email and his manager's email address
        print (email, manager)

But it doesn't return an email address and a person's manager's email.

What would be the right way to get it? (also the other way around, knowing a person's name, to get his subordinates.)

1

There are 1 answers

4
gianpa On BEST ANSWER

You may try the following snippet:

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
gal = outlook.Session.GetGlobalAddressList()
entries = gal.AddressEntries

for e in entries:
    user = e.GetExchangeUser()
    if user is not None:
        print(user.Name)
        print(user.FirstName)
        print(user.LastName)
        print(user.PrimarySmtpAddress)