60 lines
1.7 KiB
Python
Executable File
60 lines
1.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import imaplib
|
|
import email.header
|
|
import datetime
|
|
|
|
def process_mailbox(mailbox, to = None):
|
|
rv, data = mailbox.search(None, "(UNSEEN)")
|
|
if rv != 'OK':
|
|
print("No messages found!")
|
|
return
|
|
|
|
for num in data[0].split():
|
|
rv, data = mailbox.fetch(num, '(BODY.PEEK[])')
|
|
if rv != 'OK':
|
|
print("ERROR getting message", num)
|
|
return
|
|
|
|
msg = email.message_from_string(data[0][1].decode())
|
|
|
|
if to is not None:
|
|
print('To: ', to)
|
|
|
|
print('From:', msg['From'])
|
|
decode = email.header.decode_header(msg['Subject'])
|
|
subject = ''.join(map(lambda x: x[0].decode(), decode))
|
|
|
|
print('Subject: %s' % subject)
|
|
date_tuple = email.utils.parsedate_tz(msg['Date'])
|
|
if date_tuple:
|
|
local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple))
|
|
print("Local Date:", local_date.strftime("%a, %d %b %Y %H:%M:%S"))
|
|
# with code below you can process text of email
|
|
# if msg.is_multipart():
|
|
# for payload in msg.get_payload():
|
|
# if payload.get_content_maintype() == 'text':
|
|
# print(payload.get_payload())
|
|
# else:
|
|
# print(msg.get_payload())
|
|
|
|
|
|
try:
|
|
import credentials
|
|
except:
|
|
print("Couldn't read crendtials")
|
|
exit(1)
|
|
|
|
for account in credentials.accounts:
|
|
|
|
mailbox = imaplib.IMAP4_SSL(account.host, 993)
|
|
mailbox.login(account.username, account.password)
|
|
|
|
rv, data = mailbox.select("INBOX")
|
|
|
|
if rv == 'OK':
|
|
process_mailbox(mailbox, account.email)
|
|
|
|
mailbox.close()
|
|
mailbox.logout()
|