简体   繁体   中英

ImapLib get gmail id mail(python)

Sorry for my english. For example i have link like this

https://mail.google.com/mail/u/0/?source=sync&tf=1&view=pt&th=1614fcf57d5cb6ec1&search=all

where 1614fcf57d5cb6ec1 it uuid message. By this link i can view pdf mail. For work with mail i use imaplib. I try get this uuid gmail message like this:

mail.get('Message-id')

but it give me id like this: 3CB8978E-60DD-41B1-AA2E-0685219513F3@happyplugs.com

My question: Hav i can get id gmail message use imaplib?

UPD:

IMAP_SERVER = 'imap.gmail.com'
IMAP_PORT = '993'
IMAP_USE_SSL = True

 def __init__(self):
        print("MailBox __init__")
        self.user = '123@gmail.com'
        self.password = '123'
        if IMAP_USE_SSL:
            self.imap = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)
        else:
            self.imap = imaplib.IMAP4(IMAP_SERVER, IMAP_PORT)

    def __enter__(self):
        print("MailBox __enter__")
        self.imap.login(self.user, self.password)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.imap.close()
        self.imap.logout()

def get_google_id():
 self.imap.select('Inbox')
status, response = self.imap.search(None, 'ALL')

        if status == 'OK':
            items = response[0].split()
            for email_id in reversed(items):
                status, response = self.imap.fetch(email_id, "(RFC822)")
                if status == 'OK':
                    email_body = response[0][1]
                    mail = email.message_from_bytes(email_body)

                    id_mess = mail.get('Message-id') //this give me not gmail mail id

imaplib has a separate set of functions to operate on IMAP UIDs.

status, response = self.imap.uid('search', None, "ALL")
for uid in reversed(response[0].split()):
    status, response = self.imap.uid('fetch, uid, '(RFC822)') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM