Skip to content

Commit

Permalink
Issue #10: Initial work on o365 integration option.
Browse files Browse the repository at this point in the history
  • Loading branch information
geerlingguy committed Feb 25, 2023
1 parent f05f76e commit 06884ff
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ To make this script work, you need to do two things on the Pi:
Then run `./email_check.py`, and prepare to be amazed! Or not, especially if it doesn't work.

> You might also need to install Pip, if you get an error on the `python3 -m pip` command. To do that, run `sudo apt install -y python3-pip`.
>
> If using Office 365, you should also install `o365` with `python3 -m pip install o365`.
### How do I continuously check the email inbox?

Expand Down
80 changes: 80 additions & 0 deletions email_check_o365.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python3

# The email checker script.

import os
import yaml
import logging
import email
import bell_slap
import play_sound
from random import randint
from time import sleep
from O365 import Account, Connection, MSGraphProtocol, Message, MailBox, oauth_authentication_flow

# 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)

# Authenticate with Office 365.
scopes = ['basic', 'message_all']
# TODO: See https://github.com/O365/python-o365#install
# TODO: See https://stackoverflow.com/a/55501390
credentials = (<secret>, <another secret>) # TODO - Get these.
account = Account(credentials = credentials)
if not account.is_authenticated:
account.authenticate(scopes = scopes)

# Get Inbox, print 25 emails.
account.connection.refresh_token()mailbox = account.mailbox()
inbox = mailbox.get_folder(folder_name='Inbox')
for message in inbox.get_messages(25):
print (message.subject)

# TODO EXIT FOR NOW.
print("Exiting early")
exit()

server = IMAPClient(config['email']['imap_host'])
server.login(config['email']['username'], config['email']['password'])

# See how many messages are in the inbox.
select_info = server.select_folder('INBOX')
logging.info('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))

from_contains = config['conditions']['from_contains']
subject_contains = config['conditions']['subject_contains']

# Process unread messages. When message is fetched, it's marked as 'seen'.
for msgid, message_data in server.fetch(messages, ['RFC822']).items():
email_message = email.message_from_bytes(message_data[b'RFC822'])
email_from = email_message.get('From')
email_subject = email_message.get('Subject')

# Check if the email from address and subject match our conditions.
if from_contains in email_from and subject_contains in email_subject:
print("Found matching email: %s\n" % email_subject)

# Slap the bell.
if config['bell']['enable']:
bell_slap.slap_the_bell()

# Play the sound.
if config['sound']['enable']:
play_sound.play_the_sound(file=config['sound']['file'])

# Sleep for a few seconds between dings.
sleep(randint(1, 5))

server.logout()

0 comments on commit 06884ff

Please sign in to comment.