from datetime import datetime import time import MySQLdb import smtplib, ssl email="josedeception00@gmail.com" port = 465 # For SSL password = "honeypot2020" # context = ssl.create_default_context() db = MySQLdb.connect(host='localhost', user = "root",passwd= 'root', db="moodle") cursor=db.cursor() notify=cursor.execute("SELECT username FROM mdl_user, mdl_logstore_standard_log where mdl_user.id = mdl_logstore_standard_log.USERID and username='dummy1' ") user=cursor.fetchone() cursor.execute("SELECT timecreated FROM mdl_logstore_standard_log WHERE USERID = '3' ORDER BY timecreated DESC LIMIT 1,1 ") newtime = cursor.fetchone() newtime=(newtime[0]) f=open("time.txt","r+") oldtime=f.readline() oldtime=int(oldtime) time=datetime.utcfromtimestamp(newtime).strftime('%m-%d-%Y %H-%M-%S') message=f"""\ Subject:decoy {time} To:{email} From:{email} {user[0]} logged in {time} """ if oldtime < newtime: with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server: server.login(email, password) server.sendmail(email ,email, message) print(user[0], " logged in ", time) f.seek(0) f.write(str(newtime)) else: print( False) cursor.close() db.close() f.close()