Python combined with window scheduling task to monitor mail

Time:2021-11-9

Speaking of automation is definitely the most obvious b-style talk after dinner. After all, freeing your hands is a gene handed down from your ancestors. It’s 2020. Do you have to log in to each email account to check email every day when you go to work? Come and unlock this chapter

Overall thinking

  1. Mail reading through Python zemail Library
  2. Save the latest email ID, and compare the email ID in the second execution to determine whether it is a new email
  3. Realize window pop-up prompt through Python pymsgbox Library
  4. Configure window scheduling tasks to be executed every 1 minute

Step 1: mail reading

Mail reading is performed through the python zemail library. Before use, it is installed through PIP install zemail. Here is an example of mail reading

import zmail
#Get the latest mail and print mail information
server = zmail.server('[email protected]', 'mdgxgiwpnkspbxgx')
mail = server.get_latest()
zmail.show(mail)
print(mail["id"])
print(mail["from"])
print(mail["to"])
print(mail["subject"])
print(mail["context_text"])
print(mail["context_html"])

When configuring the mail server, note that the POP3 / SMTP service should be enabled for the mailbox. The QQ mailbox is enabled in settings – > enable the POP3 / SMTP service and set the authorization code at the same time. The second one configured by the server is the automatically generated authorization code

Step 2: compare old and new emails

If you want to prompt the user to have the latest email, you should record the information of the last email. After the specified time, compare the information of the two emails. Here we use the email ID (note that the ID is not unique. If you want to ensure certain accuracy, you can use the ID combined with various information such as email title)

Record the mail information. I put it in a TXT file here. Each time, I read it first and then write it

old_mailid = open('id.txt', 'r').readline()
#Write new message ID
with open('id.txt', mode='w+', encoding='utf-8') as f:
    f.write(str(mail_id))

Step 3: realize window pop-up prompt

Predecessors have done this for us for a long time. There are many such libraries. Pymsgbox is used here. Pay attention to installing pymsgbox through PIP install before use

First understand the various forms of pymsgbox pop-up window

pymsgbox.alert(text='', title='', button='OK')
pymsgbox.confirm(text='', title='', buttons=['OK', 'Cancel'])
pymsgbox.pompt(text='', title='' , default='')
pymsgbox.password(text='', title='', default='', mask='*')

Obviously, we don’t need too complicated pop-up windows here. Just an alert is enough

#Determine whether the message is up-to-date
if old_mailid != str(mail_id):
    Pymsgbox.alert ("you have a new email!") # pop up to prompt for a new email

Step 4: configure window scheduling tasks

After all the above is completed, the latest email can be prompted by executing the python file. The problem now is that if it is automated, you can’t click it manually

Here, configure the window scheduling task. First, create a new task and name it autoemail

Configure the trigger and execute it every 1 minute. What is the content to be executed here? Obviously, window cannot directly execute python. Here, create a. Bat file, execute the python file in the file, and specify the. Bat file here for the planned task
Python combined with window scheduling task to monitor mail

last

Now, after you manually send an email to the mailbox, wait for 1 minute, you can see the pop-up prompt of pymsgbox. Now you can go to the mailbox to check the email, or you can directly display the email content at will
Python combined with window scheduling task to monitor mail

Want to get the full code (or other office automation code) stamp here  : ​https://github.com/yanwydxf/o…

Recommended Today

Python final review resources

Tip: blue is the required part, yellow is the prompt and unnecessary part. 1. Source code file extension * py 2. Python uses indentation as the syntax boundary, and generally uses four grid indentation. 3. Python variables and functions are defined without specifying the type.  Python variables do not need to be declared in advance […]