16.10.4 control your computer by email

Posted by hoppyite on Thu, 02 Jan 2020 04:50:56 +0100

Write a program to check the email account every 15 minutes, get all instructions sent by email and execute them automatically
The knowledge of gui automation is needed here. I use email to send download link, the program automatically extracts the connection from the mailbox, calls thunderbolt to download and delete the email.
Although I have a lot of code here, it is not difficult.

import pyautogui,time,imapclient,subprocess,pyperclip,pyzmail,smtplib
from email.mime.text import MIMEText
my_user = input('Please enter email account:')
my_password = input('Please input a password:')
my_rec=input('Please enter the email address of the recipient:')
while 1:
    imapobj=imapclient.IMAPClient('xxx',ssl=True)#xxx is the address of imap server. If it is SSL encrypted, ssl=True needs to be added
    imapobj.login(my_user,my_password)
    imapobj.select_folder('INBOX',readonly=False)
    uids=imapobj.search()
    rawmessage=imapobj.fetch(uids,['BODY[]'])
    for uid in uids:
        message = pyzmail.PyzMessage.factory(rawmessage[uid][b'BODY[]'])
        if message.get_subject().startswith('xx'):#xx is the keyword you want to fill in,
            if message.text_part!=None:
                link=message.text_part.get_payload().decode(message.text_part.charset)#Get links
                imapobj.delete_messages([uid])
                imapobj.expunge()
                imapobj.logout()
                xunlei=subprocess.Popen('D:\\Thunder\\Program\\Thunder.exe')
                time.sleep(1)
                pyperclip.copy(link)
                time.sleep(5)
                location=pyautogui.locateOnScreen('xxx')#xxx is the path of the screenshot of 'thunderbolt download'. The sample picture is below
                clocation=pyautogui.center(location)
                pyautogui.click(clocation)#Click the download now button of thunderbolt
                #Set thunderbolt to exit automatically after downloading (my thunderbolt needs to be reset every time it is opened)
                location2=pyautogui.locateOnScreen('xxx')#xxx is the path of the screenshot of 'plan task', the example picture is below
                clocation2=pyautogui.center(location2)
                pyautogui.click(clocation2)#Click the plan task button,
                time.sleep(1)#Because I use the thunderbolt Version (my thunderbolt doesn't handle keystrokes fast enough), I have to write many pyautogui.press functions and time.time functions. You can try to use a more concise method
                pyautogui.press('up')
                time.sleep(1)
                pyautogui.press('right')
                time.sleep(1)
                pyautogui.press('right')
                time.sleep(1)
                pyautogui.press('up')
                time.sleep(1)
                pyautogui.press('enter')
                #If you cannot set thunderbolt correctly, please modify the specific parameters, such as increasing the number in time.sleep(), or changing the order of keystrokes

                #Send notification mail to recipient mailbox
                smtpobj=smtplib.SMTP_SSL('xxx',465)#xxx is the smtp server address and 465 is the port number. Different mailboxes may be different
                smtpobj.ehlo()
                send_message=MIMEText('xxx','plain','utf-8')#xxx is the notification information, such as downloading
                send_message['Subject']='x'#x is the subject of the message
                send_message['From']='x'#x is the sender's name, self determined
                send_message['To']='x'#x is the name of the recipient
                smtpobj.login(my_user,my_password)
                smtpobj.sendmail(my_user,my_rec,send_message.as_string())
                smtpobj.quit()
                xunlei.wait()#Wait for thunderbolt to exit
                # Send notification mail to recipient mailbox
                smtpobj=smtplib.SMTP_SSL('xxx',465)#Ditto
                smtpobj.ehlo()
                send_message=MIMEText('xxx','plain','utf-8')#As above, notify the recipient that the download is complete
                send_message['Subject']='x'#with
                send_message['From']='x'   #
                send_message['To']='x'     #upper
                smtpobj.login(my_user,my_password)
                smtpobj.sendmail(my_user,my_rec,send_message.as_string())
                smtpobj.quit()
                break
        else:
            continue
    time.sleep(900)

Sample picture of "Thunderbolt download"

Sample picture of 'plan task'

Topics: SSL