Skip to content

Instantly share code, notes, and snippets.

@Bouni
Created November 18, 2019 16:11
Show Gist options
  • Save Bouni/c8981be88964f83ba5bf3969f98a6911 to your computer and use it in GitHub Desktop.
Save Bouni/c8981be88964f83ba5bf3969f98a6911 to your computer and use it in GitHub Desktop.
Read unseen mails to console
# -*- coding: utf-8 -*-
import os
import sys
import logging
import ssl
import email
from imaplib import IMAP4_SSL
LOGGER = logging.getLogger("mailchecker")
class Mail:
""" search and download from IMAP server """
def __init__(self, host, user, password, mailbox="INBOX"):
self.host = host
self.user = user
self.password = password
self._connect()
self._login()
self._select_mailbox(mailbox)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
if self.imap:
self.imap.close()
self.imap.logout()
def _connect(self):
""" connect to IMAP server """
try:
ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
self.imap = IMAP4_SSL(self.host, ssl_context=ssl_ctx)
except Exception as e:
LOGGER.error("Mail connection error %s", e)
sys.exit()
def _login(self):
""" login to email account """
try:
self.imap.login(self.user, self.password)
except Exception as e:
LOGGER.error("Mail Login Error: %s", e)
sys.exit()
def _select_mailbox(self, mailbox="INBOX"):
try:
self.imap.select(mailbox)
except Exception as e:
LOGGER.error("Mail select mailbox Error: %s", e)
sys.exit()
def search_mails(self, subject, unseen=True):
""" serach selected inbox for mails with a certain subject """
if unseen:
unseen = " UNSEEN"
else:
unseen = ""
sort = "REVERSE DATE"
charset = "UTF-8"
search = f'(HEADER Subject "{subject}"{unseen})'
status, data = self.imap.sort(sort, charset, search)
if data[0]:
LOGGER.info("%s neue E-Mails", len(data[0].split()))
return data[0].split()
return None
def get_mdg_body(self, msgids):
"""Get body of an email"""
if not msgids:
return None
content_types = ["text/html", "text/plain"]
for msgid in msgids:
status, data = self.imap.fetch(msgid, "(RFC822)")
msg = email.message_from_string(data[0][1].decode("utf-8"))
for part in msg.walk():
if part.get_content_type() in content_types:
print(part)
if __name__ == "__main__":
mail = Mail("imap.mailanbieter.de", "[email protected]", "Password1234")
mails = mail.search_mails("ALARMMAIL")
mail.get_mdg_body(mails)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment