在无尽的while循环中,我想每60秒发送一封带有附件的邮件。附件是包含测量设备数据的文件。这意味着数据总是在变化。该代码可以正常工作,但是每次发送电子邮件时,附件数量都会增加。这意味着当它发送第一封邮件时,它包含一个附件;当60秒后发送第二封邮件时,它包含两次附件,然后60秒后,它包含三遍附件,依此类推。它应该只有一个附件。我怎么了?
txt = MIMEText('Die heutigen Messdaten')
msg.attach(txt)
curr_time = time.time()
next_time = curr_time + 10
while True:
if (next_time >= curr_time):
curr_time = time.time()
print("do nothing")
else:
s = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port)
s.login(email_user, email_pass)
next_time = next_time + 60
curr_time = time.time()
print(next_time)
print(curr_time)
file_dir = '/home/pi/Projekte/Test1/'
file_name = 'Stromzaehler18.txt'
filename = file_dir + file_name
#filename = '/home/pi/Projekte/Test1/Stromzaehler18.txt'
fo18=open(filename,'rb')
attach =
email.mime.application.MIMEApplication(fo18.read(),
_subtype="pdf")
fo18.close()
attach.add_header('Content-
Disposition','attachment',filename=file_name)
msg.attach(attach)
s.send_message(msg)
s.quit()
print("Finished!")
s.quit()
附件数每60秒增加一次(否则…)。 附件数应始终为1。