Verified Python email sending program

Source: Internet
Author: User

# 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'

# 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 ()

If _ name _ = '_ main __':
Authinfo = {}
Authinfo ['server'] = 'smtp'
Authinfo ['user'] = '***** @'
Authinfo ['Password'] = ***** '// write your password
Fromadd = '**** @'
Toadd = ['* *** @', 'elbert. ']
Subject = 'hello, boy title'
Plaintext = 'here is plain text'
Htmltext = '<B> HTML text </B>'
Sendemail (authinfo, fromadd, toadd, subject, plaintext, htmltext)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.