Use Python imaplib and email modules to read message text content and attachment content

Source: Internet
Author: User

#! Encoding:utf8 ' Environment: WIN10 64-bit Python 2.7.5 reference: Http://www.pythonclub.org/python-network-application/email-formathttp ://blog.sina.com.cn/s/blog_4deeda2501016eyf.html "Import imaplibimport emaildef parseheader (message):" "" "to parse the message header" " "Subject = Message.get (' subject ') H = email. Header.header (subject) DH = email. Header.decode_header (h) Subject = Unicode (dh[0][0], dh[0][1]). Encode (' gb2312 ') # Theme Print subject print ' </ Br> ' # sender print ' from: ', Email.utils.parseaddr (Message.get (' from ')) [1] print ' </br> ' # recipient Print ' To: ', Email.utils.parseaddr (Message.get (' to ')) [1] print ' </br> ' # cc people print ' cc: ', EMAIL.UTILS.PARSEADDR (messag E.get_all (' cc ')) [1] def parsebody (message): "" "parses the Message/Body" "" # Loop every MIME block in the letter for part in Message.walk (): # Here to determine if it is M Ultipart, yes, the data inside is a message list if not Part.is_multipart (): CharSet = Part.get_charset () # print ' Charse     T: ', CharSet contenttype = Part.get_content_type ()   # print ' Content-type ', contenttype name = Part.get_param ("name") #如果是附件, this will take out the file name of the attachment if name: # There are attachments # three lines below the code just to decode like =?GBK? q?=cf=e0=c6=ac.rar?= This file name fh = email. Header.header (name) FDH = email. Header.decode_header (FH) fname = dh[0][0] print ' Attachment name: ', fname # attach_data = Par.get_pay Load (decode=true) # Decodes the attachment data and then stores it into the file # try: # f = open (fname, ' WB ') #注意一定要用wb来打开文件 because the attachment is generally binary Piece # except: # print ' attachment name has illegal characters, auto-change ' # f = open (' AAAA ', ' WB ') # F.writ  E (attach_data) # f.close () Else: #不是附件, is text content print part.get_payload (decode=true) # Decode the text content, direct output can be. # Pass # print ' + ' *60 # is used to differentiate the output of each part def Getmail (host, username, password, port=993): Try:serv = Imaplib. Imap4_ssl (host, port) except Exception, E:serv = Imaplib. IMAP4 (host, Port) serv.login (username, password) serv.select () # Search for mail content typ,data = Serv.search (None, ' (from "[email protected]") ') count = 1pcount = 1for num in Data[0].split () [:: -1]: Typ, DA    TA = serv.fetch (num, ' (RFC822) ') Text = data[0][1] message = email.message_from_string (text) # converted to Email.message object Parseheader (message) print ' </br> ' parsebody (message) Pcount + = 1 if pcount > count:breakserv.c Lose () serv.logout () if __name__ = = ' __main__ ': host = "imap.mail_serv.com" # "pop.mail_serv.com" username = "[email  protected]_serv.com "Password =" Your_password "Getmail (host, username, password)

Transferred from: https://my.oschina.net/dexterman/blog/177650

Use Python imaplib and email modules to read message text content and attachment content

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.