# Since this year we are going to conduct an email test, I will take a look at the e-mail's stuff and learn python. I always think that the explanatory language is a powerful tool for automated testing,
However, generally, scripting languages have the difficulty of object-oriented and are not easy to use in large projects. According to the preliminary study, Python has good object-oriented features,
Based on the study, I found that this language is indeed quite fun and attracted me. I copied a piece of code to verify it. The test passed and posted the following post.
Import email
Import mimetypes
From email. mimemultipart import mimemultipart
From email. mimetext import mimetext
From email. mimeimage import mimeimage
Import smtplib
Def sendemail (authinfo, fromadd, toadd, subject, plaintext, htmltext ):
Strfrom = fromadd
Strto = ','. Join (toadd)
Server = authinfo. Get ('server ')
User = authinfo. Get ('user ')
Passwd = authinfo. Get ('Password ')
If not (server and user and passwd ):
Print 'complete login info, exit now'
Return
# Set Root Information
Msgroot = Email. mimemultipart. mimemultipart ('related ')
Msgroot ['subobject'] = subject
Msgroot ['from'] = strfrom
Msgroot ['to'] = strto
Msgroot. Preamble = 'this is a multi-part message in MIME format .'
# Encapsulate the plain and HTML versions of the message body in
# 'Alternative 'part, so message agents can decide which they want to display.
Msgalternative = mimemultipart ('alternative ')
Msgroot. Attach (msgalternative)
# Set plain text information
# Msgtext = mimetext (plaintext, 'plain ', 'utf-8 ')
# Msgalternative. Attach (msgtext)
# Set HTML Information
Msgtext = Email. mimetext. mimetext (htmltext, 'html', 'utf-8 ')
Msgalternative. Attach (msgtext)
# Set built-in Image Information
# Fp = open('test.jpg ', 'rb ')
# Msgimage = mimeimage (FP. Read ())
# FP. Close ()
# Msgimage. add_header ('content-id', '<image1> ')
# Msgroot. Attach (msgimage)
# Send an email
SMTP = smtplib. SMTP ()
# Set the debugging level, depending on the situation
SMTP. set_debuglevel (1)
SMTP. Connect (server)
SMTP. login (user, passwd)
SMTP. Sendmail (strfrom, strto, msgroot. as_string ())
# SMTP. Sendmail (strfrom, strto, msgroot. as_string ())
SMTP. Quit ()
Return
If _ name _ = '_ main __':
Authinfo = {}
Authinfo ['server'] = 'smtp .163.com'
Authinfo ['user'] = '***** @ 163.com'
Authinfo ['Password'] = ***** '// write your password
Fromadd = '**** @ 163.com'
Toadd = ['* *** @ 163.com', 'elbert. chenh@gamil.com ']
Subject = 'hello, boy title'
Plaintext = 'here is plain text'
Htmltext = '<B> HTML text </B>'
Sendemail (authinfo, fromadd, toadd, subject, plaintext, htmltext)