Common automated test scripts - Send e-mail

# For transmitting the report or other content 

#
- * - Coding: UTF-. 8 - * - Import OS Import smtplib from email.mime.text Import MimeText from email.mime.multipart Import MimeMultipart class the SendEmail (Object): DEF the __init__ ( Self, Receivers): mail_host = " 127.0.0.1 " # the SMTP server mail_user = " username " # username mail_pass = "password " # password SENDER = "123456qq.com @ " # sender's mailbox file_path = os.path.dirname (os.path.dirname ( __FILE__ )) + ' \\ \\ test_result.html Report ' title = ' automated test results ' # message subject mail_body = " View test report, please download the attachment! ! ! " Message = MimeMultipart () Message [ ' the From ' ] = " {} " .format (SENDER) Message [ ' the To ' ] = " , " . ' the Subject ' ] = title MSGTEXT = MimeText (mail_body, _subtype = ' Plain ' , _CHARSET = ' UTF-. 8 ' ) message.attach (MSGTEXT) # Add the primary mail body content # add a HTML text attachments ff = open ( file_path, ' RB ' ) ATT = MimeText (ff.read (), ' Base64 ' , ' UTF-. 8 ' ) # accessory set the content type, set to binary ATT [ " the content-the type "] = ' The Application / OCTET-Stream ' # Set attachment head, add a file name ATT [ " Content-Disposition " ] = ' Attachment; filename = "test_result.html" ' # solve the Chinese garbled attachment name # att.add_header ( ' Disposition-the Content ',' Attachment ', filename = (' UTF-. 8 ',' ', the basename)) message.attach (ATT) ff.close () the try : smtpObj = smtplib.SMTP (mail_host, 25 ) smtpObj.login (mail_user, mail_pass) smtpObj.sendmail (SENDER, Receivers, the Message. as_string ()) # Send Print (" Message has been sent successfully " ) smtpObj.quit () the except smtplib.SMTPException AS E: Print (E) IF the __name__ == " __main__ " : the SendEmail ([ " [email protected] " , " [email protected] " ] )

 

Guess you like

Origin www.cnblogs.com/chenri/p/11365640.html