preface
In the above article, we described how to use Python+ Selenium Automated test field: Mailbox login as an example to verify a successful login test case. Wouldn’t it be hard if we had to look at the code every time we executed it? How do you do that? If the program execution error or successful test case, send me an email notification, WE do not need to open the code to check the execution result, just pay attention to their own mail every day, the failed part, to raise bugs or modify the code
How to automatically send SMTP mail in Python code?
First, preparation
You need to authorize the SMTP of the mailbox to be sent and generate an authorization code. The password you enter for sending emails is the authorization code. The enabling method is as follows:
Take QQ mailbox as an example:
– > Settings – > account drop-down to “POP3 / IMAP/SMTP/Exchange/CardDAV/CalDAV service”
POP3/SMTP service (how to use Foxmail and other software to send and receive mail?) — click Open and generate an authorization code (Note: to enable authorization, send an SMS message). The generated result is shown in the figure
Before we write the code, let’s first think about it:
1. What do we need to enter to send an email?
(1) Title
(2) Content
(3) Who will send it?
(4) To whom?
2. What is the sending process?
(1) Establish mailbox connection protocol
(2) To log in to the mailbox, the authorization code of the sender is required
(3) Click Send
Three, after sorting out the idea, how do we achieve it?
1. Enter data
(1) For the title of the mail, the module Import Header needs to be introduced and the Header() method in it is used
(2) The content of the email needs to import the module MIMEText and use the function MIMEText() to construct the content data
(3) It is not clear who will send it, but you can use a request: from_email=input(‘ Please enter sender email name :’)
(4) To whom, to_email=input(‘ Please input recipient email ‘)
2. Sending process:
(1) Establish mailbox connection protocol:
STMP = smtplib.smtp (), set up a connection: stmp.connect(‘smtp.qq.com’,25)
(2) To log in to the mailbox, the authorization code of the sender is required
Authorization code:pwd=input('Please enter the authorization code :') Login: stmp.login(from_email,pwd)Copy the code
(3) Click send: stmp.sendmail()
Four, after the above analysis can be implemented code
1. First I put the previous loginEmail loginThe code in this article is packaged as a function
from selenium import webdriverimport timedef email_login(url):Driver = webdriver.chrome ()# open the browser driver.maximize_window()# maximize the browser driver.get(url)# enter the url and send the request, Driver.find_element_by_id ("lbNormal").click() driver.switch_to.frame(driver.find_element_by_xpath("//div/div[3]/div[4]/div/div/iframe"))# Driver.find_element_by_name ("email").send_keys(" username of your email") driver.find_element_by_name("password").send_keys(" password of your email") Driver.find_element_by_id ("dologin").click() driver.switch_to.default_content()# Exit frame driver. Implicitly_wait (5)# Result =driver.find_element_by_class_name("nui-tree-item-text"). Text # Print (result) if result==' inbox ': Print (" this test case passes ") real_result=" pass "else: Sleep (5)# driver.quit()# return real_resultCopy the code
2. After encapsulation is completed, call and write the contents to send the email and execute the sending function (as shown in the figure) :
from email.mime.text import MIMEText Header import header # Import smtplib # Send email from public_class import email_public28 Def create_email_msg() def create_email_msg() Real_result =email_public28.email_login("https://mail.126.com/")# Enter the actual argument MSG = MIMEText(' The result of executing the use case is: '+ real_result, 'plain', 'utF-8 ')# create the content of the message, type 'plain', character encoding utF-8 MSG ['Subject']=Header(" ",'utf-8')# return msg# return title and content # def send_email_msg(): From_email =input(' Please enter sender email address :') PWD =input(' Please enter authorization code :') # authorization code not password to_email=input(' Please enter recipient email address ') STMP = smtplib.smtp () Stmp.login (from_email, PWD)# real_result = create_email_msg() # real_result = create_email_msg() Sendmail (from_email,to_email,real_result.as_string()) # send email stmp.quit() # close the connection print(' email sent successfully ')if __name__ = = "__main__ ': send_email_msg # () function calls to send mailCopy the code