Skip to content

Instantly share code, notes, and snippets.

@teward
Last active July 10, 2020 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save teward/aa798cedb8292df2cd4ae13dfb2ff470 to your computer and use it in GitHub Desktop.
Save teward/aa798cedb8292df2cd4ae13dfb2ff470 to your computer and use it in GitHub Desktop.
A script for testing IMAP logins. Simply edit SSL_ENABLED to determine if you need SSL or not, IMAP_PORT if you aren't using a standard IMAP or IMAPS port, USERNAME, PASSWORD, and IMAP_SERVER to all suit your own setup. Then run the script. It'll tell you if login worked or not.
#!/usr/bin/python3
import imaplib
# If you need to use SSL/TLS encrypted secure connections,
# then set this to True
SSL_ENABLED = False
# Change this to be your IMAP server hostname.
IMAP_SERVER = 'hostname'
# Change this if you listen on a nonstandard-port.
# 143 (non-SSL/TLS) and 993 (SSL/TLS) are standard ports.
# Don't change if you use those ports.
IMAP_PORT = None
# Change these accordingly.
USERNAME = 'username'
PASSWORD = 'password'
if __name__ == "__main__":
try:
print("Connecting to mail server...")
if SSL_ENABLED:
conn = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT if IMAP_PORT else 993)
else:
conn = imaplib.IMAP4(IMAP_SERVER, IMAP_PORT if IMAP_PORT else 143)
print("Connected to server.\n")
except:
print("Could not connect to mail server.\n")
exit(1)
try:
print("Attempting to login...")
data = conn.login(USERNAME, PASSWORD)
if data[0].lower() == 'ok':
print("Logged in successfully.\n")
else:
raise Exception("Login failure.\n")
except:
print("Could not login to mail server. Make sure you are using the proper credentials.\n")
exit(2)
print("Connected and logged in without issue.")
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment