Skip to content

Instantly share code, notes, and snippets.

@gabaruga
Created September 28, 2019 17:42
Show Gist options
  • Save gabaruga/eb4162d6af6cd4c840f8570b6f3be6c2 to your computer and use it in GitHub Desktop.
Save gabaruga/eb4162d6af6cd4c840f8570b6f3be6c2 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, ServiceAccount, \
EWSDateTime, EWSTimeZone, Configuration, NTLM, CalendarItem, Message, \
Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, \
HTMLBody, Build, Version
import os
import datetime
import time
debug = False
def main():
if debug: print("Debug: Mode enabled")
credentials = Credentials(username='tester@contoso.com', password='qwerty')
try:
account = Account(primary_smtp_address='tester@contoso.com', credentials=credentials, autodiscover=True, access_type=DELEGATE)
if debug: print("Debug: EWS bind succesfull")
except:
print("Error: EWS binding failed")
return 2
# Send test email using timestamp as a key
subject = str(datetime.datetime.now())
try:
if (os.system('/usr/bin/printf "%b" "To: tester@contoso.com\nSubject:'+subject+'\n\n" | /usr/sbin/ssmtp -F"Tester" tester@contoso.com 2>/dev/null')) != 0:
raise ValueError("Email was not send")
if debug: print("Debug: Email sent")
except:
print("Error: Email send failed")
return 2
# Mail check cycle
retry_count = 10
retry_interval = 2
time.sleep(1) # Wait a bit for a message to arrive
for x in range(retry_count):
if account.inbox.filter(subject__exact=subject).count() == 1:
print("Success: Mail received")
return 0
else:
if debug: print("Debug: Iteration #"+str(x))
time.sleep(retry_interval)
print("Error: Timed out")
return 2
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment