Question

How to find all emails with attachments in GMail account using IMAP access? I'm on Python but is there only way to do this is to check each message?

Thanks

Was it helpful?

Solution

You may want to check out the IMAPClient library which gets rid of a lot of the frustrations of dealing with IMAP.

This is how you would get emails with attachments using IMAPClient:

from imapclient import IMAPClient

HOST = 'imap.gmail.com'
USERNAME = ''
PASSWORD = ''
ssl = True

server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)

server.select_folder('INBOX')
messages = server.search(['X-GM-RAW has:attachment'])

print '%d messages with attachments' % (len(messages),)
print
print 'UIDs of messages with attachments:'
print messages

Result:

1 messages with attachments

UIDs of messages with attachments:
[85L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top