Skip to content

Instantly share code, notes, and snippets.

@dsprenkels
Created January 20, 2017 15:52
Show Gist options
  • Save dsprenkels/cebcbd5f4ce88bfd57384ab60a86779e to your computer and use it in GitHub Desktop.
Save dsprenkels/cebcbd5f4ce88bfd57384ab60a86779e to your computer and use it in GitHub Desktop.
example script for scraping IMAP mailboxes
#!/usr/bin/env python3
import getpass
import email
import imaplib
HOST = 'dsprenkels.com'
USER = 'hello@dsprenkels.com'
PASSWD = getpass.getpass()
MAILBOX = 'Voorraadcie.Reserveringen'
conn = imaplib.IMAP4_SSL(HOST)
typ, _ = conn.login(USER, PASSWD)
assert typ == 'OK'
typ, count = conn.select(MAILBOX)
assert typ == 'OK'
print("Downloading {} messages".format(count))
typ, data = conn.search(None, 'ALL')
assert typ == 'OK'
messages = []
for num in data[0].split():
typ, data = conn.fetch(num, '(BODY[TEXT])')
assert typ == 'OK'
print('Message %s\n%s\n' % (num, data[0][1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment