Skip to content

Instantly share code, notes, and snippets.

@nfl0
Created September 9, 2021 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfl0/8854f619a9f40c9b058b61e9f9ad2c4a to your computer and use it in GitHub Desktop.
Save nfl0/8854f619a9f40c9b058b61e9f9ad2c4a to your computer and use it in GitHub Desktop.
##### config.yml #####
email:
username: darcoders77@gmail.com
password: roddy2023
imap_host: imap.gmail.com
imap_port: 993
##### end of config.yml #####
##### imap.py #####
#!/usr/bin/env python3
# The email checker script.
import os
import yaml
import logging
import email
from time import sleep
from imapclient import IMAPClient
# Store where we currently are in the filesystem.
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
# Attempt to read in the configuration.
with open(os.path.join(__location__, "config.yml"), 'r') as stream:
try:
config = yaml.load(stream, Loader=yaml.FullLoader)
except yaml.YAMLError as exc:
print(exc)
# Log into email server.
server = IMAPClient(config['email']['imap_host'])
server.login(config['email']['username'], config['email']['password'])
while True:
try:
# See how many messages are in the inbox.
select_info = server.select_folder('INBOX')
logging.info('Messages in INBOX: %d' % select_info[b'EXISTS'])
print('Messages in INBOX: %d' % select_info[b'EXISTS'])
# See if there are any new messages.
messages = server.search('UNSEEN')
logging.info("Unread messages: %d\n" % len(messages))
print("Unread messages: %d\n" % len(messages))
sleep(10)
except KeyboardInterrupt:
break
server.logout()
##### end of imap.py #####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment