In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, Xiaobian will bring you about how Python can send monitoring emails regularly. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.
Whether in the credit area or the payment area, as a risk officer, we need to monitor the deployed strategy model. The credit area may also need to monitor the overdue performance of customers. At this time, if we can use python to automatically connect to the database, analyze and process the data such as strategy, model, and post-loan performance, and output standard tables or pictures to fixed folders. Then use python to automatically send monitoring emails to leaders and colleagues on a regular basis, so that relevant personnel can timely update the running status of the policy model and the overdue dynamics of the project, which is a very meaningful thing.
Xiaobian focuses on how to use python to connect mailboxes and automatically send emails. As for python connection database for data processing module, I will share it with you later.
I. Details of automatic timing task operation
First, let's take a look at the operation details of automatic scheduled task sending mail, video link
Open POP3/SMTP service
When using python to connect to the mailbox, you need to open POP3/SMTP service. This article takes qq mailbox as an example to explain. Other mailbox opening methods please use Baidu. First log in to the web version of qq mailbox-> click settings-> click accounts-> drop-down to find POP3/IMAP/SMTP/ Exchange/CardDAV/CalDAV service.
Then turn on POP3/SMTP service (follow the instructions).
Opening POP3/SMTP service is mainly to get the token used in the code, which is a string of text similar to a string. Remember to save it in the process of opening the service.
III. Send mail
When using python to send emails, smtplib and email libraries are mainly used. These two libraries are self-contained and can be imported directly.
1 import library import osimport emailimport smtplibimport datetimefrom email.header import Headerfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipart#Load library os.chdir(r"E:\Automated Mail") #Set address of attachment file when sending mail
The smtplib module is mainly responsible for sending emails, and the email module is mainly responsible for constructing emails. Remember to replace the file path in os.chdir with the path where you store your email attachments.
2 Set the mail content
Usually we send emails, mainly fill in the recipient, email subject, email body, attachments and other information. Using python to send an email is also filling in this information, but it is indicated in the statement.
mail_sender = "2275885700@qq.com" #sender email name mail_license = '***************' #The sender email authorization code, i.e. the token obtained by opening POP3/SMTP service, needs to be replaced with your email mail_host = "smtp.qq.com" # SMTP server, here is qq mailbox, if it is 163 mailbox, please replace qqmail_receivers = ["227585700@qq.com","wawoxzy@163.com"] #recipient mailbox mail = MIMEMultipart ('related ') #Set mail body mail["From"] = "sender_name" #Set sender email ["To"] = "receiver_1_name,receiver_2_name" #Set recipient email subject_content = '[Daily] Risk Monitoring' #Set mail subject mail["Subject"] = Header(subject_content,'utf-8') #Add mail subject body_content = """Hello, everyone This is Risk Monitoring Daily, please check! Best Regards, thanks XX""" #Set message body message_text = MIMEText(body_content,"plain","utf-8") #Set text content, text format, encoding method mail.attach(message_text) #Add text objects to MIMEMultipart objects
Note: The value in mail_license needs to be replaced with the token you obtained when you started the POP3/SMTP service. The contents of the above sentences can be adjusted according to needs. If you have any questions, please refer to the remarks below.
3 Add attachments
Attachment information is generally pictures and data files, so this article describes these two kinds of attachments, you just need to change the name to your attachment name.
image_picture = open('yk2.jpg','rb') #Open attachment picture message_image = MIMEImage(image_picture.read()) #Set binary data to read #message_image.add_header ('Content-ID','') message_image["Content-Disposition"] = 'attachment; filename="yk2.jpg"' #Set attachment image name mail.attach(message_image) #Add image to email to image_picture.close() #Close the image you just opened mail.attach(message_image) #Add pictures as email attachments atta = MIMEText(open ('BlackFriday. csv','rb').read(),' base64','utf-8')#Add data (csv, excel, etc.) attachments atta["Content-Disposition"] = 'attachment; filename="BlackFriday.csv"' #Set data (csv, excel, etc.) attachment name mail.attach(atta) #Add csv attachment to email Go 4 Send email
After the email body and attachments are set, you can send the email. The specific sentence is as follows:
smtp = smtplib.SMTP() #Create SMTP object smtp.connect(mail_host, 25) #Set the domain name and port of the sender mailbox, the port address is 25smtp.set_debuglevel(1) #Print all messages interacting with SMTP server smtp.login(mail_sender,mail_license) #Send email according to email address and email receiver at least login email smtp.sendmail(mail_sender, mail_receivers, mail.as_string()) #Send email and set the email content format to strnow_time = datetime.datetime.now ().strftime ('% Y-%m-% d') #Get current time print(now_time+"Daily monitoring email sent successfully") #Print a record of successfully sending mail on a certain day smtp.quit() #Close SMTP object
So far, the email has been sent, and then we look at the effect of email sending it.
5 Email delivery effects
The email sent by python is the same as the email sent by us manually, as shown in the following picture.
Next, let's look at how to set up timed tasks to send emails at regular times every day.
IV. Set up timed tasks
In the previous two sections, we explained how to send mail automatically with python. This section describes how to set up timed tasks to grab the contents of fixed files at fixed times.
1 Specific steps for setting up timed tasks
First, click the search menu in the lower left corner of windows desktop, enter "control panel," and the control panel will appear in the best match, click control panel.
Then, enter the word "task" in the search box of the control panel, and the management tool and scheduled task will appear. Click scheduled task.
Then, click Create Basic Task, fill in the name and description (customizable), click Next to default to daily, if you want to change to weekly, you can choose by yourself.
Click Next to set the time for the task to start, click Next to select Start Program, and then click Next. Then click Browse, select Python script that needs timing, and finally click Finish.
The above is how Python, which is shared by everyone, can send monitoring emails regularly. If there are similar doubts, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to 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.