1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import time import os import smtplib from email.mime.text import MIMEText from email.header import Header class Mylog: def __init__(self): time_info=str(time.strftime("%m_%d_%H", time.localtime())) if not os.path.exists('./runlogs/'): os.mkdir('./runlogs/') self.file_name='./runlogs/'+time_info+'.log' self.file=open(self.file_name, 'a') def __del__(self): self.file.close() def add_log(self,lg): time_info = str(time.strftime("%H-%M-%S --> ", time.localtime())) self.file.write(time_info+lg) self.file.write('\r') self.file.flush() def send_mail(self): self.file.close() from_addr = '' to_addrs = '' qqCode = '' smtp_server = 'smtp.qq.com' smtp_port = 465 stmp = smtplib.SMTP_SSL(smtp_server, smtp_port) stmp.login(from_addr, qqCode) with open(self.file_name,'r') as f: buffer=f.read() message = MIMEText(buffer, 'plain', 'utf-8') message['From'] = Header("autodl", 'utf-8') message['To'] = Header("me", 'utf-8') subject = 'AUTODL-运行结束' message['Subject'] = Header(subject, 'utf-8') try: stmp.sendmail(from_addr, to_addrs, message.as_string()) print('邮件发送成功') except Exception as e: print('邮件发送失败--' + str(e)) 使用时,用add_log替换print函数,最后在os.system('shutdown')前调用send_mail函数。
|