Skip to content

Instantly share code, notes, and snippets.

@tuck1s
Created March 9, 2021 15:42
Show Gist options
  • Save tuck1s/6de2cf9377d6d04a27b7b9803584042e to your computer and use it in GitHub Desktop.
Save tuck1s/6de2cf9377d6d04a27b7b9803584042e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import imaplib, os
# Very simple IMAP client - prints messages in inbox
imap_host = os.getenv('IMAP_HOST')
imap_user = os.getenv('IMAP_USER')
imap_pass = os.getenv('IMAP_PASSWORD')
# connect to host using SSL
imap = imaplib.IMAP4_SSL(imap_host)
## login to server
imap.login(imap_user, imap_pass)
imap.select('Inbox')
print('Reading messages from {} for user {}'.format(imap_host, imap_user))
tmp, data = imap.search(None, 'ALL')
for num in data[0].split():
tmp, data = imap.fetch(num, '(RFC822)')
if tmp == 'OK':
id = ' Message {0} '.format(num.decode('utf-8)'))
sep = '='
print(sep * 10 + id + sep *(100-len(id)))
message = data[0][1].decode('utf-8')
print(message)
else:
print(tmp)
imap.close()
@tuck1s
Copy link
Author

tuck1s commented Mar 9, 2021

Set up the environment variables before running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment