Skip to content

Instantly share code, notes, and snippets.

@israel-dryer
Created September 22, 2020 01:35
Show Gist options
  • Save israel-dryer/f45da6c27cd3330d59152689bb6238a0 to your computer and use it in GitHub Desktop.
Save israel-dryer/f45da6c27cd3330d59152689bb6238a0 to your computer and use it in GitHub Desktop.
Add members to an Outlook distribution
"""
Add members to an existing user distribution list in Python
https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.recipients
https://docs.microsoft.com/en-us/office/vba/api/outlook.distlistitem.addmembers
https://docs.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders
"""
import win32com.client as client
# start an instance of outlook
outlook = client.Dispatch('Outlook.Application')
# get the namespace and default contacts folder (10)
namespace = outlook.GetNameSpace('MAPI')
contacts = namespace.GetDefaultFolder(10)
# get the distribution list by name from the contact items
distro = contacts.Items['MyTestDistro']
# create a temp mail item to hold recipients
temp_obj = outlook.CreateItem(0)
recipients = temp_obj.recipients
# add a few recipients to the recipients item
recipients.Add('person_1@outlook.com')
recipients.Add('person_2@outlook.com')
recipients.Add('person_3@outlook.com')
# resolve the address to match with any existing contacts
recipients.ResolveAll()
# add a members to distribution
distro.AddMembers(recipients)
distro.Save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment