Python selenium2 sample-Email send

Source: Internet
Author: User
Tags base64 format message all mail

Objective

In the practice of daily automated testing, we always need to send the relevant information such as records, results and so on in the test process to the relevant people through automatic means. Python's smtplib, email module provides us with a very good e-mail delivery and other functions of implementation.

Plain Text Mail

In general, we need to send a large number of plain text mail notifications, or send a summary test report, the use of such a Send method, the sample code is as follows:

#-*-Coding:utf-8-*-

__author__ = U ' bitter leaves '

Import Smtplib

From Email.mime.text import Mimetext

From Email.header Import Header

if __name__ = = ' __main__ ':

sender = U ' [email protected] ' # Sender email address

Receiver = U ' [email protected] ' # Recipient email address

Subject = U ' python email text message send test '

SmtpServer = U ' smtp.163.com ' # SMTP Service

Username = U ' testname ' # sender mail user name or dedicated to SMTP account user name

Password = u ' testpassword ' # sender email password or password dedicated to SMTP account

msg = mimetext (u ' hello ', ' text ', ' Utf-8 ') # Text Format message body contents

msg[' Subject ' = header (Subject, ' utf-8 ') # message header

SMTP = Smtplib. SMTP () # Initializes an SMTP object

Smtp.connect (' smtp.163.com ') # Connecting to an SMTP server

Smtp.login (username, password) # Sign in to the SMTP service

Smtp.sendmail (sender, receiver, msg.as_string ()) # Send mail

Smtp.quit () # Close connection when send is complete

Messages in HTML form

Often, we often generate test reports or records in HTML format, if sent in a text mail, HTML-formatted reports or records will also display the HTML tags, so that the message recipients can see the HTML format of the report, you need to send the message, Configure the corresponding parameters so that the mail client can parse the HTML-formatted message correctly, as shown in the following example:

#-*-Coding:utf-8-*-

__author__ = U ' bitter leaves '

Import Smtplib

From Email.mime.text import Mimetext

From Email.header Import Header

if __name__ = = ' __main__ ':

sender = U ' [email protected] ' # Sender email address

Receiver = U ' [email protected] ' # Recipient email address

Subject = U ' python email e-mail send test '

SmtpServer = U ' smtp.163.com ' # SMTP Service

Username = U ' testname ' # sender mail user name or dedicated to SMTP account user name

Password = u ' testpassword ' # sender email password or password dedicated to SMTP account

msg = mimetext (u '

msg[' Subject ' = header (Subject, ' utf-8 ') # message header

SMTP = Smtplib. SMTP () # Initializes an SMTP object

Smtp.connect (' smtp.163.com ') # Connecting to an SMTP server

Smtp.login (username, password) # Sign in to the SMTP service

Smtp.sendmail (sender, receiver, msg.as_string ()) # Send mail

Smtp.quit () # Close connection when send is complete

Mail with Attachments

can text and HTML-formatted messages meet your needs? Looking back, is there a lot of attachments to be sent during the test? Are there many in the automated testing process? Wait a minute.... Yes, we also need to send an email with attachments to meet our daily testing needs, and here's an example of sending an email with an attachment:

#-*-Coding:utf-8-*-

__author__ = U ' bitter leaves '

Import Smtplib

From Email.mime.text import Mimetext

From Email.header Import Header

if __name__ = = ' __main__ ':

sender = U ' [email protected] ' # Sender email address

Receiver = U ' [email protected] ' # Recipient email address

Subject = U ' python email attachment send test '

SmtpServer = U ' smtp.163.com ' # SMTP Service

Username = U ' testname ' # sender mail user name or dedicated to SMTP account user name

Password = u ' testpassword ' # sender email password or password dedicated to SMTP account

msg = Mimemultipart (' attachment ')

msg[' Subject ' = Header (Subject, ' utf-8 ')

#构造附件

ATT = mimetext (open (' c:\\1.jpg ', ' RB '). Read (), ' base64 ', ' Utf-8 ') # reading attachments

att["Content-type"] = ' application/octet-stream '

att["content-disposition"] = ' attachment; Filename= "1.jpg"

Msg.attach (ATT) # Associated Attachments

##############################################

SMTP = Smtplib. SMTP ()

Smtp.connect (' smtp.163.com ')

Smtp.login (username, password)

Smtp.sendmail (sender, receiver, msg.as_string ())

Smtp.quit ()

Mass Mailing

In the above example, all mail is received by a single person, in the actual application, we need to send a group of people mail, see the example below:

#-*-Coding:utf-8-*-

__author__ =u ' bitter leaves '

Importsmtplib

Fromemail.mime.textimportMIMEText

Fromemail.headerimportheader

if__name__ = = ' __main__ ':

# Sender Email Address

Sender =u ' [email protected] '

# BULK Recipient Email ADDRESS!!!!!

receiver = [u ' [email protected] ', u ' * * @xx. com ', u ' * * @yy. com ']

# message Header

Subject =u ' python email mass mailing test '

# SMTP Service

SmtpServer =u ' smtp.163.com '

# Sender Mail user name or user name dedicated to SMTP account

Username =u ' TestName '

# Sender Email Password or password dedicated to an SMTP account

Password =u ' Testpassword '

# Text Format message body contents

msg = mimetext (u ' Hello Mass ', ' text ', ' Utf-8 ')

# message Header

msg[' Subject ' = Header (Subject, ' utf-8 ')

# Initialize an SMTP object

SMTP = Smtplib. SMTP ()

# Connect to an SMTP server

Smtp.connect (' smtp.163.com ')

# Sign in to the SMTP service

Smtp.login (username, password)

# Send mail

Smtp.sendmail (sender, receiver, msg.as_string ())

# Close Connection when send is complete

Smtp.quit ()

Comprehensive example

All of the examples above are one by one demos by functional classification, and the following example includes all of the above features:

#-*-Coding:utf-8-*-

__author__ = U ' bitter leaves '

Import Smtplib

Form Email.mime.multipart Import Mimemultpart

From Email.mime.text import Mimetext

From Email.image import Mimeimage

if __name__ = = ' __main__ ':

# define some connection data

Sender = u "[Email protected]"

receiver = [u ' [email protected] ', u ' [email protected] '

Subject = u "Mail Synthesis Example"

Username = u "username"

Password = u "Password"

# Create a message

msg = Mimemultipart (' alternative ')

msg[' Subject '] = u "Test"

# Send Content

Text = u "Hello, this is textual content"

html = u "" "

Test report

Test Results Overview

"""

# Add MIME type

Parttext = Mimetext (text, U ' Plain ')

parthtml = Mimetext (HTML, u ' HTML ')

Msg.attach (Parttext)

Msg.attach (parthtml)

#构造附件

Attach = Mimetext (open (' c:\\demo.jpg '). Read (), ' base64 ', ' Utf-8 ')

attach[' content-type '] = ' application/octet-stream '

attach[' content-disposition ' = ' attachment;filename= ' demo.jpg "'

Msg.attach (Attach)

# Send mail

SMTP =smtplib. SMTP ()

Smtp.connect (' smtp.163.com ')

Smtp.login (Username,password)

Smtp.sendmail (sender, receiver, msg.as_string ())

Smtp.quit ()

Conclusion

This article describes the use of the Python email module to send messages from text messages, HTML-formatted messages, attachment messages, and the three-way combination.

Python selenium2 sample-Email send

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: info-contact@alibabacloud.com 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.