Python primary learning-based remote control program for sending and receiving mail

Source: Internet
Author: User

Disclaimer: This essay comes from the Geek College Python learning through computer control, but the content is not the same, the realization of the idea is a process-oriented, abandoned a lot of things. (such as log printing and so on, there is no analysis, interested readers can go to geek College to find teaching video to see)

Brief analysis of Python remote control applet function:

1: The ability to open the computer you want to open files, such as open a photo, play a music, run QQ and so on.

2: Can call computer system command shutdown and so on.

The knowledge to be used:

Module:OS (System command)

Win32API (Open PC file) (requires download module Pywin32), can be downloaded directly from PIP. Attached API Documentation Link Http://docs.activestate.com/activepython/2.7/pywin32/win32api.html

Time (causes the program to wait)

Re (regular expression, get message header and sender)

Poplib (offer to receive mail agreement POP3)

Smtplib (SMTP for sending mail protocol)

Core code: Os.system ("command"); Win32API. ShellExecute (0, ' open ', Command, ', ', ', 0),remail (Host,username,password) (accepts the mail function),Send_mail ( tolist,sub,content) (send mail function)

----------------------------------I'm a split line---------------------------------------------------------------------------------- -------------

What is the SMTP protocol?

SMTP is an application-layer protocol supported by the TCP protocol that provides reliable and efficient e-mail transport. SMTP is a mail service built on TCP that is used primarily to transmit mail messages between systems and to provide notification of letters

What is the POP3 protocol?

The POP protocol supports "offline" mail processing. The process is that the message is sent to the server, and the e-mail client invokes the mail client program to connect to the server and downloads all unread e-mails. This offline access mode is a store-and-forward service that sends mail from the mail server to a personal terminal machine, typically a PC or MAC. Once the message is sent to a PC or Mac, the Mail on the mail server will be deleted. However, most of the current POP3 mail servers can "download mail only, server side does not delete", that is, the improved POP3 protocol.

The text is difficult to understand, there is a picture of the truth, SMTP protocol and POP3 protocol operation diagram (from the book, everyone will)

Well, the writing level is limited! That's a lot of crap, on the code.

#Coding:utf-8ImportPoplibImportReImportSmtplibImportWin32APIImport Time fromEmail.mime.textImportMimetexthost='pop.sina.com'         #POP server addressmail_host='smtp.sina.com'  #SMTP Server addressUsername ='[email protected]' #User namePassword ='xxxxx'                    #PasswordMail_usr='xxxxxx'                    #Usermail_postfix='sina.com'           #suffix namemailto_list=['[email protected]']#Accept the list, there's no use here#Accept mail FunctionsdefRemail (Host,username,password): Pop_conn=Poplib. Pop3_ssl (host) Pop_conn.user (username) pop_conn.pass_ (password) RET=pop_conn.list () mailbody=pop_conn.retr (Len (ret[1])) Subject=re.search ("Subject: (. *?) ',", str (mailbody[1]). Decode ('Utf-8'), Re. S). Group (1) Sender=re.search ("X-sender: (. *?) ',", str (mailbody[1]). Decode ('Utf-8'), Re. S). Group (1)    PrintSender,subjectreturnSender,subject#Send mail functiondefSend_mail (tolist,sub,content): Me=' Self'+'<'+mail_usr+'@'+mail_postfix+'>'msg=mimetext (content,_subtype='Palin', _charset='Utf-8') msg['Subject']=Sub msg[' from']=Me msg[' to']=';'. Join (ToList)Try: Server=Smtplib. SMTP () server.connect (mail_host) server.login (Mail_usr,password) server.sendmail (me,tolist,msg.as_str ING ()) server.close ()returnTrueexceptexception,e:PrintStr (e)returnFalse#Execute CommanddefExecute (Command): Win32API. ShellExecute (0,'Open', command,"',"', 1)#RundefRun (Host,username,password): whileTrue:sender,subjcet=remail (Host,username,password)#get the first message, get sender information, message subject        ifsender=='[email protected]':#only a specific mailbox sent over will execute the commandExecute (SUBJCET) send_mail (Mailto_list,'Stop',"')#send yourself to prevent the command from being executed again        ifsender=='[email protected]':             BreakTime.sleep (5) Run (Host,username,password)

Note: If you want to implement the applet above to run successfully, you must open the SMTP/POP3 protocol, which can be opened in the mailbox settings. At present, the pro-test can be used is Sina mailbox, other mailboxes such as QQ mailbox, NetEase mailbox, such as non-client login need client authorization code (do not know what is self-Baidu, here do not explain), more trouble, so did not try. Interested students, can try, if successful also hope to tell a voice, extremely grateful.

Summarize:

Silently put it running in the roommate's computer, when he played the game is fascinated, suddenly the computer pop up a photo of Zhen son, brain fill that picture is drunk.

Through this little program, I learned about the email reception protocol, and some APIs I never knew, which made me more interested in exploring the world of Python.

Life is short, I use Python.

Python primary learning based remote control program for sending and receiving messages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.