Guest User

Untitled

a guest
Jan 18th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import getpass
  2. from collections import defaultdict
  3. from exchangelib import Account, Configuration, Credentials, DELEGATE
  4.  
  5. def connect(server, email, username, password):
  6. """
  7. Get Exchange account cconnection with server
  8. """
  9. creds = Credentials(username=username, password=password)
  10. config = Configuration(server=server, credentials=creds)
  11. return Account(primary_smtp_address=email, autodiscover=False, config = config, access_type=DELEGATE)
  12.  
  13. def print_tree(account):
  14. """
  15. Print folder tree
  16. """
  17. print(account.root.tree())
  18.  
  19. def get_recent_emails(account, folder_name, count):
  20. """
  21. Retrieve most emails for a given folder
  22. """
  23. # Get the folder object
  24. folder = account.root / 'Top of Information Store' / folder_name
  25. # Get emails
  26. return folder.all().order_by('-datetime_received')[:count]
  27.  
  28. def count_senders(emails):
  29. """
  30. Given emails, provide counts of sender by name
  31. """
  32. counts = defaultdict(int)
  33. for email in emails:
  34. counts[email.sender.name] += 1
  35. return counts
  36.  
  37. def print_non_replies(emails, agents):
  38. """
  39. Print subjects where no agents have replied
  40. """
  41. dealt_with = dict()
  42. not_dealt_with = dict()
  43. not_dealt_with_list = list()
  44. for email in emails: # newest to oldest
  45. # Simplify subject
  46. subject = email.subject.lower().replace('re: ', '').replace('fw: ', '')
  47.  
  48. if subject in dealt_with or subject in not_dealt_with:
  49. continue
  50. elif email.sender.name in agents:
  51. # If most recent email was from an agent it's been dealt with
  52. dealt_with[subject] = email
  53. else:
  54. # Email from anyone else has not been dealt with
  55. not_dealt_with[subject] = email
  56. not_dealt_with_list += [email.subject]
  57.  
  58. print('NOT DEALT WITH:')
  59. for subject in not_dealt_with_list:
  60. print(' * ', subject)
  61.  
  62. def main():
  63. # Connection details
  64. server = 'outlook.office365.com'
  65. email = 'bob.hope@test.com'
  66. username = 'bobh@test.com'
  67. password = getpass.getpass()
  68.  
  69. account = connect(server, email, username, password)
  70.  
  71. # Print folder tree
  72. #print_tree(account)
  73.  
  74. emails = get_recent_emails(account, 'Inbox', 50)
  75.  
  76. # Count senders by From name
  77. #counts = count_senders(emails)
  78. #print(counts)
  79.  
  80. # Identify emails without a response
  81. agents = {
  82. 'John':True,
  83. 'Jane':True,
  84. }
  85. print_non_replies(emails, agents)
  86.  
  87. if __name__ == '__main__':
  88. main()
Add Comment
Please, Sign In to add comment