# L-21 MCS 507 Mon 9 Oct 2023 : emailauth.py
"""
Python 3 script to authenticate the user with a google email.
"""
from smtplib import SMTP
from getpass import getpass
try:
    SERVER = SMTP(host='smtp.gmail.com', port=587)
    RESULT = SERVER.starttls()
    print(RESULT)
except:
    print('Failed to connect to the server.')
SUCCESS = False
USER = input('Type your account : ')
USER = USER + '@gmail.com'
for _ in range(3):  # allow for three attempts
    PASW = getpass('Type your app password : ')
    try:
        RESULT = SERVER.login(USER, PASW)
        print(RESULT)
        SUCCESS = True
        break
    except:
        print('Login failed, please try again.')
if SUCCESS:
    print('You are authenticated via gmail.')
else:
    print('Authentication via gmail failed.')
