-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemails.py
More file actions
33 lines (23 loc) · 1.01 KB
/
emails.py
File metadata and controls
33 lines (23 loc) · 1.01 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
import datetime as dt
import logging
import pythoncom
import win32com.client
from azure.functions import Blueprint, TimerRequest
import utils
email_app = Blueprint()
@email_app.timer_trigger(schedule="0 */30 * * * *", arg_name="mytimer",
run_on_startup=True,
use_monitor=False)
def upload_mails_to_cosmos(mytimer: TimerRequest):
pythoncom.CoInitialize()
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace(
"MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
# Gets message from the previous week
lastWeekDateTime = dt.datetime.now() - dt.timedelta(days=7)
lastWeekDateTime = lastWeekDateTime.strftime(
'%m/%d/%Y %H:%M %p') # <-- This format compatible with "Restrict"
messages = messages.Restrict("[ReceivedTime] >= '" + lastWeekDateTime + "'")
logging.info(f"Total number of emails: {len(messages)}")
utils.transform_and_update_to_cosmos(messages)