Python email instance, return only the information of the first message __python

Source: Internet
Author: User
Tags base64

Python receive mail sample

Import Base64
Import Imaplib
From Email.mime.text import Mimetext
Import Email

Import Base64
import imaplib from
email.mime.text import mimetext Import  
Email

def get_first_unread_email_title_body (self): "" Gets the first title and content return value for unread messages: Subj: The title of the first message, no message returns none Bdy: The contents of the first message, no message returns none "" "conn = Imaplib. Imap4_ssl ("smtp.xxxx.com.cn", 993) Conn.login ("username", "password") conn.select () typ, data = Co
                Nn.search (None, ' unseen ') subj =[] bdy=[] try:for num in Data[0].split (): Typ, msg_data = Conn.fetch (num, ' (RFC822) ') for Response_part in Msg_data:if ISI
                        Nstance (Response_part, tuple): msg = email.message_from_string (response_part[1]) #获取消息体 subject=msg[' subject '] #print subject Sub J.append (subject) Payload=msg.get_payload () Body=self.extract_body (payload ) #lens = Len (body) #print lens, "message leNgth "#lenx = lens-(lens%4 if lens%4 else 4) #try:
                        #result = base64.decodestring (Body[:lenx]) #except: #pass Print body#base64.decodestring (body) bdy.append Typ, RESPO
            NSE = Conn.store (num, ' +flags ', R ' (\seen) ') finally:print "finished!" Try:conn.close () Except:pass conn.logout () If Len (bdy) &G
            T;0:print Subj[0],bdy[0] return subj[0],bdy[0] else:print ' none ', ' none ' Return ' None ', ' none '

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.