I need to create a Python script which grabs different information about 1500 Outlook Contacts (out of 20000), based on their email. Until now, I managed to do this:
def grab_user_details(email):
first_name, last_name, department, location = '', '', '', ''
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
gal = outlook.Session.GetGlobalAddressList()
entries = gal.AddressEntries
for i in entries:
user = i.GetExchangeUser()
if user is not None:
if user.PrimarySmtpAddress == email:
first_name = email.split("@")[0].split(".")[0].title()
last_name = email.split("@")[0].split(".")[1].title()
department = user.Department
location = user.OfficeLocation
print(email, first_name, last_name, department, location)
break
In the end, the code just iterates through the GAL for that specific email. After finding it, it breaks, and continues searching for the next email. This method is fast for emails starting with A, or at least B... but when you have a GAL with 20000 emails, you can't just wait 2 days for it to finish the whole alphabet.
Is there a faster way of doing this?
Thanks!
Use the CreateRecipient method of the
Namespaceclass to get an instance of the Recipient class based on the email address.The Recipient.AddressEntry property returns the
AddressEntryobject corresponding to the resolved recipient. Accessing theAddressEntryproperty forces resolution of an unresolved recipient name. If the name cannot be resolved, an error is returned. If the recipient is resolved, theResolvedproperty isTrue.Then you can use the AddressEntry.GetExchangeUser method which returns an ExchangeUser object that represents the
AddressEntryif theAddressEntrybelongs to an ExchangeAddressListobject such as the Global Address List (GAL) and corresponds to an Exchange user.