Search code examples
pythonemailgmailimaplib

imaplib save authenticate pythone


Sorry for my english. I use imaplib for email operations. I try save authenticate, for save my password. I found method

IMAP4.login_cram_md5

but how it use?

This is my example

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


    class MailBox(object):
        def __init__(self, user, password):
            print("MailBox __init__")
            self.user = user
            self.password = password
            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_cram_md5(self.user, self.password)
            return self

start code

class Main:
    main_box = MailBox('asdasd@gmail.com', 'wqeqwdsadwqeq==')
    main_box.__enter__()

i have error

imaplib.error: Unsupported AUTHENTICATE mechanism. i188mb2545835lji

Solution

  • Your server doesn't support CRAM_MD5.

    Check the CAPABILITY of a server before you try to use an extension:

    > a CAPABILITY   
    < * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=OAUTHBEARER AUTH=XOAUTH
    < a OK Thats all she wrote!
    

    Gmail supports Authenticate methods XOAUTH2, PLAIN, PLAIN-CLIENTTOKEN, OAUTHBEARER, and XOAUTH (plus the baseline non-authenticate LOGIN).

    PLAIN is standard. I'm not sure what PLAIN-CLIENTTOKEN is. The rest are variants of OAUTH.