In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how Python can automatically send email. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
What does python mean Python is a cross-platform, interpretive, compiled, interactive and object-oriented scripting language, originally designed to write automated scripts, and is often used to develop independent and large-scale projects as versions are constantly updated and new features are added.
come to the point
In the process of automated testing, the general test results will be sent to the relevant personnel in the form of e-mail, so, in Python, how to write code to send mail to the corresponding users?
At the same time, there are different forms of email, such as text, HTML, picture attachments, non-picture attachments and so on. How do you operate these? Next, let's learn ~
Introduction to automatic email sending SMTP
SMTP, known as simple Mail transfer Protocol in Chinese, is a protocol that provides reliable and efficient e-mail transmission, which can transmit mail information between systems.
SMTP is a mail service based on FTP file transfer service, which is mainly used for the transmission of mail information between systems and provides notification about letters.
SMTP is independent of a specific transmission subsystem and only needs reliable and orderly data flow channel support. One of the important features of SMTP is that it can transmit mail across the network, that is, "SMTP mail relay".
Using SMTP, mail transmission between the same network processing process can be realized, and mail transmission between a processing process and other networks can also be realized through a repeater or gateway.
We take QQ Mail as an example. If you need to send an email, you must first start the SMTP service.
Enable the SMTP service
Log in to QQ Mail, click Settings; Click on account
Click the POP3/SMTP service to be enabled, and follow the steps to generate the authorization code, which is usually 16 digits.
Following the above steps, the SMTP service can be easily started.
Property configuration of message
Create a new Python file and write code:
# mailbox attribute configuration # mailbox server mailserver = 'smtp.qq.com' # sender-this mailbox is a random userName_SendMail =' 66666666roomqq.com'# mailbox sender authentication code-defines the recipient of the email for the sender mailbox according to the authorization code generated in step 4 userName_AuthCode = 'abcdefghijklmnop'#-I wrote it casually, if there are more recipients You can use a list to express received_mail = ['88888888888 / qq.composure] if QQ Mail, the server is smtp.qq.com, and if it is 163mailbox, the server is smtp.163.com. For other mailboxes, you can query them by Baidu. Send a text email
Let's send a simple text email first.
Guide package:
You need to use the SMTPLIB library to connect to the mailbox import smtplib. The library that handles the content of the message: email.mine, and you need to import: from email.mime.text import MIMEText to send text mail.
# need to use the SMTPLIB library to connect to mailboxes, import smtplib# libraries that process email content Email.minefrom email.mime.text import MIMEText# mailbox attribute configuration # mailbox server mailserver = 'smtp.qq.com' # sender-this mailbox is a random userName_SendMail =' 66666666roomqq.com'# mailbox sender authentication code-defines the recipient of the message for the sender mailbox according to the authorization code generated in step 4 userName_AuthCode = 'abcdefghijklmnop'#-I write it casually, if there are more recipients You can use a list to express received_mail = ['88888888888' qq.compositional content'] # send a simple email to deal with the content of the email = 'this is a pure text message! This is a pure text message!' # definition of email content in plain text, operated through MIMEText. Plain is the default display form of text email = MIMEText (content, 'plain',' utf-8') email ['Subject'] =' this is the subject of the message'# define email subject email ['From'] = userName_SendMail # Sender email [' To'] =', '.join (received_mail) # recipient (multiple can be added) If there is only one recipient, you can write the mailbox number directly) # send email # QQ Mail's port number is 465.The port number of other mailboxes can be Baidu, not QQ Mail. You can generally use SMTP. You don't need SSLsmtp = 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 (' awesome). Email sent successfully') send HTML email
Sending other messages is similar to the above code, the mailbox property configuration and sending mail part do not change, only need to modify the processing part of the message content.
Change MIMEText (content, 'plain',' utf-8') to MIMEText (content, 'HTML',' utf-8'), and plain is the default form of text presentation.
# need to use the SMTPLIB library to connect to mailboxes, import smtplib# libraries that process email content Email.minefrom email.mime.text import MIMEText# mailbox attribute configuration # mailbox server mailserver = 'smtp.qq.com' # sender-this mailbox is a random userName_SendMail =' 66666666roomqq.com'# mailbox sender authentication code-defines the recipient of the message for the sender mailbox according to the authorization code generated in step 4 userName_AuthCode = 'abcdefghijklmnop'#-I write it casually, if there are more recipients You can use a list to express received_mail = ['88888888888 / qq.composer] # send an email with HTML content content = "".
This is an email in HTML text.
Click here to enter Baidu
"" email = MIMEText (content, 'HTML',' 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 email # QQ Mail's port number is 465. The port number of other mailboxes can be Baidu, not QQ Mail, and SMTP can be used generally. There is no need for SSLsmtp = 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 (' how impressive, the email was sent successfully') to send an attachment message
Send the attachment mail, the mailbox sends the form to email = MIMEMultipart (), and carries on the processing to the sending attachment, handles the mail attachment needs 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
# need to use the SMTPLIB library to connect to mailboxes and import smtplib# to handle email attachments Need to import MIMEMultipart,Header MIMEBasefrom email.mime.multipart import MIMEMultipartfrom email.header import Headerfrom email.mime.base import MIMEBasefrom email import encoders # Encoding format # mailbox attribute configuration # mailbox server mailserver = 'smtp.qq.com' # sender-this mailbox is my random userName_SendMail =' 6666666roomqq.com'# mailbox sender authorization code-the authorization code generated by the sender mailbox according to step 4 userName_AuthCode = 'abcdefghijklmnop'# definition -the recipient of the email.-I wrote it casually. If there are more recipients You can use a list to express received_mail = ['88888888888' qq.composer,''999999999''] # send attachment # attachment configuration mailbox email = MIMEMultipart () email ['Subject'] =' this is the subject of the message _ not a picture attachment'# define the subject of the message email ['From'] = userName_SendMail # Sender email [' To'] =', '.join (received_mail) # recipient (multiple can be added If there is only one recipient Can write mailbox number directly) # attachment processing att = MIMEBase ('application',' octet-stream') # specification att.set_payload (open ('test file .txt', 'rb'). Read ()) att.add_header (' Content-Disposition', 'attachment', filename=Header (' test file .txt', 'gbk'). Encode () encoders.encode_base64 (att) email.attach (att) # send email smtp = smtplib.SMTP_SSL (mailserver) Port=465) # QQ Mail's port number is 465 Other mailboxes can have their own port numbers of Baidu, non-QQ Mail, and generally use SMTP. There is no need for SSLsmtp.login (userName_SendMail, userName_AuthCode) smtp.sendmail (userName_SendMail,', '.join (received_mail), email.as_string ()) smtp.quit () print (' awesome, email sent successfully') the above is how to automatically send email with the Python shared by the editor, if you happen to have similar doubts It may be understood with reference to the above analysis. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.