aboutsummaryrefslogtreecommitdiffstats
path: root/python/exchange.py
blob: 18c886696dc89a45f4f4dd2546004e81bec5c006 (plain)
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
from exchangelib import Credentials, Configuration, Account, DELEGATE
from pathlib import Path
import json


def get_account(email_address):
    with open(Path('.credentials') / (email_address + '.json')) as fh:
        creds = json.load(fh)
    credentials = Credentials(**creds)
    config = Configuration(server='autodiscover.lmcg.com', credentials=credentials)
    return Account(primary_smtp_address=email_address, config=config,
                   autodiscover=False, access_type=DELEGATE)

class ExchangeMessage:
    _account = get_account("ghorel@lmcg.com")

    def get_msgs(self, count=None,
                 path=['GS', 'Swaptions'], **filters):
        folder = self._account.inbox
        for p in path:
            folder /= p
        folder = folder.filter(**filters)
        if count:
            for msg in folder.all().order_by('-datetime_sent')[:count]:
                yield msg
        else:
            for msg in folder.all().order_by('-datetime_sent'):
                yield msg

    def send_email(self, subject, body, to_recipients,
                   cc_recipients):
        m = Message(account=self._account,
                    folder=self._account.sent,
                    subject=subject,
                    body=body,
                    to_recipients=to_recipients,
                    cc_recipients=cc_recipients)
        m.send_and_save()