Faster search of email in GAL using Python

615 views Asked by At

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!

2

There are 2 answers

0
Chiser Alexandru On

Based on @Eugene Astafiev's answer (thank you!), I came up with the following code:

def grab_user_details(email):
    name, department, location = '', '', ''
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    user = outlook.Session.CreateRecipient(email)
    user.Resolve()
    print(user.Resolved)
    try:
        name = user.AddressEntry.GetExchangeUser().Name
        department = user.AddressEntry.GetExchangeUser().Department
        location = user.AddressEntry.GetExchangeUser().OfficeLocation
        print(email, name, department, location)
    except:
        print("user NA")

This method is way faster than searching through the GAL.

There's one more thing I need to fix: unfortunately, some users have 2 email address', and .CreateRecipient(email) returns nothing, although outlook can find it. I need to dig into this a little bit more.

0
Eugene Astafiev On

Use the CreateRecipient method of the Namespace class to get an instance of the Recipient class based on the email address.

Sub TestRecipient()
 Dim myNamespace As Outlook.NameSpace 
 Dim myRecipient As Outlook.Recipient 
 
 Set myNamespace = Application.GetNamespace("MAPI") 
 Set myRecipient = myNamespace.CreateRecipient("[email protected]") 
 
 myRecipient.Resolve 
 
 If myRecipient.Resolved Then 
 
   ' do some stuff here
 
 End If 
 
End Sub 

The Recipient.AddressEntry property returns the AddressEntry object corresponding to the resolved recipient. Accessing the AddressEntry property forces resolution of an unresolved recipient name. If the name cannot be resolved, an error is returned. If the recipient is resolved, the Resolved property is True.

Then you can use the AddressEntry.GetExchangeUser method which returns an ExchangeUser object that represents the AddressEntry if the AddressEntry belongs to an Exchange AddressList object such as the Global Address List (GAL) and corresponds to an Exchange user.