First, get straight to the point
During automated testing, test results are usually sent to the relevant person in the form of an email. How do you write the code in Python to send the email to the corresponding user?
At the same time, there are different forms of email, such as text, HTML, image attachment, non-image attachment, etc., how to operate these? Let’s learn to learn……
Two, automatic mail SMTP
1, the introduction
SMTP: SMTP is a protocol that provides reliable and efficient E-mail transmission. SMTP is a mail service based on FTP file transfer service. It is mainly used to transfer mail information between systems and provide notification about letters. SMTP is independent of specific transmission subsystem and only needs reliable and orderly data flow channel support. One of the important characteristics of SMTP is that it can transmit mails across the network, namely “SMTP mail relay”. Using SMTP, mail can be transmitted between the same network processing process, or between a process and other networks through a repeater or gateway (source: Baidu Bai).
Chinese name: simple mail transfer protocol function: transfer mail messages between systems
Take QQ mailbox as an example. To send emails, the SMTP service must be enabled first.
2. Enable the SMTP service
(1) Log in to QQ mailbox and click Settings;
(2) Click the account;
(3) POP3/SMTP service is clicked as enabled;
(4) Generate the authorization code according to the steps, and the authorization code is usually 16 bits;Following the above steps, SMTP service is easy to start.
3. Configuration of email properties
Create a new Python file to write the code:
# mailServer = 'smtp.qq.com' # sender - this email is my random userName_SendMail = '[email protected]' # UserName_AuthCode = 'abcdefghijklmnop' # Define the recipient of the email - I wrote it randomly, if there are many recipients, Available list represents received_mail = ['[email protected]','[email protected]']Copy the code
For QQ email, the server is smtp.qq.com; for 163 email, the server is smtp.163.com. For other email addresses, you can search them on Baidu by yourself.
Send text emails
To test the waters, let’s send a simple text email.
Guide package:
You need to use the SMTPLIB library to connect to the mailbox import SMTPLIB
Email library: email.mine. To send text emails, import: from email.mime.text import MIMEText
# import SMTPLIB # import SMTPLIB # Email.mine from email.mime.text import MIMEText # email server = 'smtp.qq.com' # sender - THIS email is written by me UserName_SendMail = '[email protected]' # Email sender authorization code - userName_AuthCode = 'abcDefghijklmnop' # Received_mail = ['[email protected]','[email protected]'] # Send a simple email, Content = 'This is a pure text message content! This is a pure text message! '# definition of message content in plain text, via MIMEText, Email = MIMEText(content, 'plain', 'UTF-8 ') email['Subject'] =' this is the Subject of the message '# Define the Subject of the message email['From'] = userName_SendMail # Sender email['To'] = Join (received_mail) # Received_mail # Port 465 for sending mail No need for SSL SMTP = smtplib.SMTP_SSL(mailServer, port=465) SMtp.login (userName_SendMail, UserName_AuthCode) smtp.sendmail(userName_SendMail, ','. Join (received_mail), email.as_string()) SMtp.quit () print(' Good! Email sent successfully ')Copy the code
5. Send HTML emails
The code for sending other emails is similar to the above code. The property configuration of the mailbox and the part of sending emails do not change, but only the part of processing the email content needs to be modified. Change MIMEText(content, ‘plain’, ‘UTF-8 ‘) to MIMEText(Content, ‘HTML’,’ UTF-8 ‘). Plain is the default text presentation.
# import SMTPLIB # import SMTPLIB # Email.mine from email.mime.text import MIMEText # email server = 'smtp.qq.com' # sender - THIS email is written by me UserName_SendMail = '[email protected]' # Email sender authorization code - userName_AuthCode = 'abcDefghijklmnop' # Define the recipient of the email. - I'm just writing casually, but if there are more recipients, Received_mail = ['[email protected]','[email protected]'] # Send an HTML message content = """ <p> This is an HTML text message </p> <p><a Href ="http://www.baidu.com"> </a></p> "" 'UTF-8 ') email['Subject'] =' this is the Subject of the message _HTML' # Define the Subject of the message email['From'] = userName_SendMail # Sender email['To'] = ','. Join (received_mail) # Recipient # Send mail # The port number of the QQ mailbox is 465. No need for SSL SMTP = smtplib.SMTP_SSL(mailServer, port=465) SMtp.login (userName_SendMail, UserName_AuthCode) smtp.sendmail(userName_SendMail, ','. Join (received_mail), email.as_string()) SMtp.quit () print(' Good! Email sent successfully ')Copy the code
6. Send email attachments
Email attachments, the email form to email = MIMEMultipart (), and to send the attachment for processing, dealing with email attachments you need to import the MIMEMultipart, Header, MIMEBase
Guide package:
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.base import MIMEBase
Import SMTPLIB # Handle email attachments, You need to import the MIMEMultipart, Header, MIMEBase from email. Mime. Multipart import MIMEMultipart from email. The Header import Header from Email.mime. base import MIMEBase from email import encoders # Email.mime. base import MIMEBase from email import encoders # Email.mime. base import MIMEBase from email import encoders # UserName_AuthCode = 'abcDefghijklmnop' userName_AuthCode = 'abcDefghijklmnop' # Define the recipient of the email - I write casually, if there are many recipients, Available list represents received_mail = ['[email protected]','[email protected]'] # Send attachment in mail # Attachment configuration email = MIMEMultipart() Email ['Subject'] = 'this is the Subject of the message _ not picture attachment' # Define the Subject of the message email['From'] = userName_SendMail # Sender email['To'] = Join (received_mail) # Recipient Att = MIMEBase('application', 'octet-stream') # att.set_payload(open(' test file.txt ', 'rb').read()) att.add_header(' content-disposition ', 'attachment', filename=Header(' testfile.txt ', Encode () encoders. Encode_base64 (att) email. Attach (att) # Send mail SMTP = smtplib.smtp_ssl (mailServer, Port =465) # SMTP =465 SSL SMtp.login (userName_SendMail, userName_AuthCode) SMtp.sendmail (userName_SendMail, ','. Join (received_mail), Email.as_string ()) SMtp.quit () print(' Email sent successfully ')Copy the code
Seven, afterword.
If this article is helpful to you, please leave your heart carefully!! Refueling oh