I cannot search sent emails in Gmail with Python

后端 未结 5 966
[愿得一人]
[愿得一人] 2020-12-28 20:51

I am trying to search for messages in the Sent (actually i care for both) but I only get incoming messages. For the time being i have

imap_conn.select()
str_         


        
相关标签:
5条回答
  • 2020-12-28 21:39

    Man, the error message is so misleading. What it's really saying is that you have tried to select an invalid folder name hence the search operation fails.

    To verify/check the current valid folders/labels do something like:

    Using ImapClient

    from imapclient import IMAPClient
    ## Connect, login and select the INBOX
    imap_conn = IMAPClient('imap.gmail.com', use_uid=True, ssl=ssl)
    imap_conn.login(USERNAME, PASSWORD)
    
    print(imap_conn.list_folders())
    

    Using imaplib

    import imaplib
    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.login('myusername@gmail.com', 'mypassword')
    print(mail.list())
    

    After I could see what folder names it was expecting, all was well.

    0 讨论(0)
  • 2020-12-28 21:39

    You need to use: imap_conn.select('[Gmail]/Sent Mail')

    Just wanted to point this out for future users who see this. It's hidden in the comments.

    0 讨论(0)
  • 2020-12-28 21:46
    import imaplib
    obj = imaplib.IMAP4_SSL('imap.gmail.com',993)
    obj.login('userid','password')
    obj.select('Sent')  # <-- response like ('OK', ['74']) --> total no. of mail in sent folder
    obj.uid('SEARCH',None,'All') # <-- Returns the list of uids of the sent folder.
    
    0 讨论(0)
  • 2020-12-28 21:48

    Make sure you use the extra quotes in your string:

    imap_conn.select('"[Gmail]/Sent Mail"')  
    

    That worked for me.

    0 讨论(0)
  • Need to use print imap_conn.list(). Tags are language based. for example in spanish is [Gmail]/Todos

    0 讨论(0)
提交回复
热议问题