I log Tencent E-mail to receive mail at record with python
First, use python receive mail must first be three categories
imaplib used to receive mail
imaplib which we note a few ways:
login to use clear text passwords identify the client. The password will be referenced.
select Select a mailbox. The data returned is the mailbox message count ( EXISTS
response). The default mailbox Shi 'INBOX'
. If you set the read-only flag is not allowed to modify the mailbox.
Search mailbox search to find matching messages
BeautifulSoup for parsing text / html and txet / plain type
email used to resolve e-mail
email which method:
message_from_string right message into a string
walk This walk()
method is a common generator, and for all sub-portions in a portion of the depth-first traversal order iterative message object tree. You typically be walk()
used as for
the loop iterator ; each iteration returns the next sub-section.
get_content_type return mail content type
get_payload returns the current payload, which will be a list of Mess
age
Second, the line and the code
from imaplib Import IMAP4_SSL from smtplib Import SMTP_SSL from BS4 Import BeautifulSoup Import Email class AutoEmail:
DEF __init__ (Self): # Recipients self.receiver = None # Sender self.sender = "E-mail account " self.host = ' % s.exmail.qq.com ' self.password = ' authorization code ' DEF ReceiverEmail (Self):
# Tencent mail server connected email_server = IMAP4_SSL (= Host self.host% ' IMAP ' ) # sign mailbox email_server.login (User = self.sender, password = self.password) # select whether the setting is read a mailbox inbox email_server.select = ( ' INBOX ' , Readonly = True) # Find messages message ALL is therefore Status, emailList = email_server.search (None, ' ALL ' )
emailist = emailList [0] .split () # select the first few messages Last = emailist [len (emailist) -. 8 ] # Get message status tuple and returns a message body Status, emaildata = email_server.fetch (Last, ' (the RFC822) ' ) # is converted into a Message object to parse messages = email.message_from_string (emaildata [0] [. 1] .decode ( ' . 8-UTF ' )) # Print (messages) Import Base64 for Data in messages.walk (): # Print (data.get_content_type ()) IF data.get_content_type () == ' text / Plain ' : # Print (Data. get_payload ())
charset = data.get_content_charset()
data = data.get_payload(decode=True).decode(charset)
print(data)
soup = BeautifulSoup(base64.b64decode(data.get_payload()), 'lxml').text
print(soup)
elif data.get_content_type() == 'application/octet-stream':
import re
if data.get('Content-Disposition', None):
Content_Disposition = data['Content-Disposition']
filename = re.search('filename="(.*)"$', Content_Disposition).group(1)
if filename.startswith('='):
filename_encode = re.match('=\?(.*)\?B', filename).group(1)
filename_base = re.search('B\?(.*)\?=$', filename).group(1)
filename_base64 = base64.b64decode(filename_base)
filename_decode = filename_base64.decode(filename_encode)
with open(filename_decode, 'wb') as file_base:
file_base.write(base64.b64decode(data.get_payload()))
if __name__ == '__main__':
autoemail = AutoEmail()
autoemail.ReceiverEmail()
Well I do not write directly on the line to see to understand, too difficult to adjust the format, there is no easy road to edit