This repository was archived by the owner on Aug 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailreader.py
More file actions
64 lines (54 loc) · 1.9 KB
/
mailreader.py
File metadata and controls
64 lines (54 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import argparse
import logging
import os
import sys
from auth import SecretStore
from mailhandler import DispatchRules
from mailhandler import O365Mailbox
from datetime import date
import requests
from urllib3.exceptions import InsecureRequestWarning
# Suppress the warnings from urllib3
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
parser = argparse.ArgumentParser()
parser.add_argument('--debug', help="Enable debugging", action='store_true')
parser.add_argument('--mailbox', help="Which mailbox to read", required=True)
parser.add_argument('--vault', help="Get account credentials from this vault KV mount")
parser.add_argument('--date', help="Parse emails as-of date")
args = parser.parse_args()
_log = logging.getLogger(__file__)
loglevel = logging.INFO
if args.debug:
loglevel=logging.DEBUG
_log.setLevel(level=loglevel)
logging.basicConfig(format='%(asctime)s <%(name)s:%(levelname)s> %(message)s',
level=loglevel)
fromdate = None
if args.date:
try:
fromdate = date.fromisoformat(args.date)
except Exception:
raise
secrets = None
if args.vault:
ss = SecretStore(loglevel=loglevel)
secrets = ss.get(args.vault)
else:
secrets = []
secrets['TENANT_ID'] = os.environ.get('ARM_TENANT_ID')
secrets['CLIENT_ID'] = os.environ.get('ARM_CLIENT_ID')
secrets['CLIENT_SECRET'] = os.environ.get('ARM_CLIENT_SECRET')
rules = DispatchRules('rules.yaml', loglevel=loglevel)
mailbox = O365Mailbox(tenant=secrets.get('TENANT_ID'),
client_id=secrets.get('CLIENT_ID'),
client_secret=secrets.get('CLIENT_SECRET'),
user_id=args.mailbox,
loglevel=loglevel)
messages = mailbox.list(date=fromdate)
for msg in messages:
try:
processed = rules.process(msg)
if processed:
mailbox.delete(msg)
except Exception as e:
raise