r1299597
r1299597

Reputation: 629

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: [email protected]

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 = '[email protected]'
        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

Upvotes: 0

Views: 1284

Answers (1)

tripleee
tripleee

Reputation: 189387

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)') 

Upvotes: 1

Related Questions