Last active
October 14, 2017 13:08
-
-
Save obradovic/33d59362ac167f033829492290c32a8b to your computer and use it in GitHub Desktop.
gets email addresses from an exchange folder and writes them to a google sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import timedelta | |
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, ServiceAccount, \ | |
EWSDateTime, EWSTimeZone, Configuration, NTLM, CalendarItem, Message, \ | |
Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, \ | |
HTMLBody, Build, Version | |
from nameparser import HumanName | |
def parsename(str): | |
name = HumanName(str) | |
return '%s, %s' % (name.last.capitalize(), name.first.capitalize()) | |
credentials = Credentials(username='XXXXXXX\\xxxxxx', password='xxxxxxx') | |
account = Account(primary_smtp_address='xxxxx@xxxxx.com', credentials=credentials, autodiscover=True, access_type=DELEGATE) | |
root = account.root | |
archive = root / 'Top of Information Store' / '2017-18 Archive' | |
archive.name | |
archive.folder_class | |
archive.total_count | |
archive.unread_count | |
name_to_email = {} | |
senders = archive.all().values('sender') | |
for x in senders: | |
name = parsename(x['sender'].name) | |
email = x['sender'].email_address.lower() | |
name_to_email[name] = email | |
print name + ': ' + email | |
# account.root.refresh() | |
# account.primary_smtp_address | |
# account.inbox.unread_count | |
# print(account.root.tree()) | |
# Dont turn on logging yet | |
# import logging | |
# from exchangelib.util import PrettyXmlHandler | |
# logging.basicConfig(level=logging.DEBUG, handlers=[PrettyXmlHandler()]) | |
# Open and read the google sheet | |
import gspread | |
from oauth2client.service_account import ServiceAccountCredentials | |
scope = ['https://spreadsheets.google.com/feeds'] | |
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope) | |
gc = gspread.authorize(credentials) | |
worksheet = gc.open("2018 Candidate Tracker").worksheet("Candidates") | |
col1 = worksheet.col_values(1) | |
ss_names = filter(None, col1) | |
i = 0 | |
for x in ss_names: | |
i += 1 | |
name = parsename(x) | |
email = name_to_email.get(name) | |
if not email: | |
print 'Email not found for %s' % name | |
continue | |
print 'Updating ' + name + ' to be ' + email | |
worksheet.update_cell(i, 11, email) | |
# Or, if you want a copy in e.g. the 'Sent' folder | |
message = Message( | |
account=account, | |
folder=account.sent, | |
subject='Xxxxxxx', | |
to_recipients=[Mailbox(email_address='xx@xxxxxxxx.com')] | |
) | |
message.body = HTMLBody('<html><body>Hello happy <b>user</b>!</body></html>') | |
message.send_and_save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment