-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecamessage_dispatcher.py
90 lines (65 loc) · 2.6 KB
/
decamessage_dispatcher.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# ---------------------------------------------------------------------------------------------------------------------#
# decamessage_dispatcher.py
#
# This module sends ten orders' worth of emails from the Drafts folder.
#
# ---------------------------------------------------------------------------------------------------------------------#
import calendar
import win32com.client
from configparser import ConfigParser
from pathlib import Path
from time import sleep
def main():
# This block opens Outlook and defines the relevant folders.
config_path = Path(__file__).parent.absolute().joinpath('config.ini')
config = ConfigParser()
config.read(config_path)
primary_mailbox = str(config['Outbot Options']['primary_mailbox'])
outlook = win32com.client.Dispatch('Outlook.Application')
mapi = outlook.GetNamespace('MAPI')
draftbox = mapi.Folders[primary_mailbox].Folders['Drafts']
outbox = mapi.Folders[primary_mailbox].Folders['Outbox']
count = 0
# This bit closes the preview pane because attempting to send a draft that is being previewed causes an error.
# If it was open, it will reopen at the end for the user's convenience.
preview_pane_open = outlook.ActiveExplorer().IsPaneVisible(3)
outlook.ActiveExplorer().ShowPane(3, False)
while count < 10:
messages = draftbox.Items
messages.Sort("[ReceivedTime]", Descending=False)
if messages:
try:
message_name = messages[0].Subject
except IndexError:
print('\nAll drafts sent!')
return
if 'FILM' not in message_name and 'RDX' not in message_name:
count += 1
messages[0].Send()
print(message_name, 'sending...')
while len(outbox.Items):
sleep(1)
print(message_name, 'sent!')
else:
break
messages = draftbox.Items
messages.Sort("[ReceivedTime]", Descending=False)
if messages:
try:
message_name = messages[0].Subject
except IndexError:
print('\nAll drafts sent!')
return
if 'RDX' in message_name:
messages[0].Send()
print(message_name, 'sending...')
while len(outbox.Items):
sleep(1)
print(message_name, 'sent!')
if count == 10:
print('\nBatch sent!')
else:
print('\nAll drafts sent!')
if preview_pane_open:
outlook.ActiveExplorer().ShowPane(3, True)
main()