1 #messages that send HTML content2 Importsmtplib, time, OS3 fromEmail.mime.textImportMimetext4 fromEmail.headerImportHeader5 6 7 defsend_mail_html (file):8 " "Sending HTML content messages" "9 #Send MailboxTenSender ='[email protected]' One #Receive Mailbox AReceiver ='[email protected]' - #Send Message subject -t = time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()) theSubject ='Automated Test Results _'+T - #Send Mailbox Server -SmtpServer ='192.168.20.190' - #Send email user/password +Username ='Zhangkai' -Password ='123456' + A #Read HTML file contents atf = open (file,'RB') -Mail_body =F.read () - f.close () - - #Assemble message content and title, Chinese required parameter ' utf-8 ', single byte character not required -msg = Mimetext (Mail_body, _subtype='HTML', _charset='Utf-8') inmsg['Subject'] = Header (subject,'Utf-8') -msg[' from'] =Sender tomsg[' to'] =receiver + #sign in and send mail - Try: theSMTP =Smtplib. SMTP () * Smtp.connect (smtpserver) $ smtp.login (username, password)Panax Notoginseng Smtp.sendmail (sender, receiver, msg.as_string ()) - except: the Print("message failed to send! ") + Else: A Print("message sent successfully! ") the finally: + smtp.quit () - $ $ defFind_new_file (dir): - " "find the latest files in the directory" " -File_lists =Os.listdir (dir) theFile_lists.sort (key=LambdaFn:os.path.getmtime (dir +"\\"+fn) - if notOs.path.isdir (dir +"\\"+fn)Wuyi Else0) the #print (' latest file: ' + file_lists[-1]) -File = Os.path.join (dir, file_lists[-1]) Wu Print('full file path:', file) - returnfile About $ -DIR ='D:\\test_data\\auto_test_result' #Specify the file directory -File = Find_new_file (dir)#find the latest HTML file -send_mail_html (file)#Sending HTML content messages
Python messages that send HTML content