-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendmail_win_hb.py
180 lines (156 loc) · 6.69 KB
/
sendmail_win_hb.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
# coding=utf-8
import glob
import click
import os
import json
import datetime
import re
import csv
from requests.exceptions import ConnectionError
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, ServiceAccount, \
EWSDateTime, EWSTimeZone, Configuration, NTLM, CalendarItem, Message, \
Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, \
HTMLBody, Build, Version
from exchangelib.errors import ErrorItemNotFound
TO_REGISTER = 'Confirmed (to register)'
def dump_csv(res, output_filename, from_date):
keys = res[0].keys()
final_output_filename = '_'.join(['Output_sendmail',
output_filename,
from_date.strftime('%y%m%d'),
datetime.datetime.now().strftime('%H%M')
]) + '.csv'
with open(final_output_filename, 'w', newline='', encoding='utf-8') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(res)
@click.command()
@click.option('--email', default='[email protected]')
@click.option('--days', default=-0.5, type=float)
# @click.option('--hours', default=-12, type=int)
def sendmail_win_hb(email, days):
# target_filename = filename + '*.csv'
# newest = max(glob.iglob(target_filename), key=os.path.getctime)
# print('newest file: ' + newest)
# today_date = datetime.datetime.now().strftime('%y%m%d')
# try:
# newest_date = re.search( filename + '(\d+)', newest).group(1)
# except AttributeError:
# newest_date = ''
# print('newest date: ' + newest_date)
# if newest_date != today_date:
# print('Error: newest date != today date.. mannual intervention needed..')
# return
try:
sendmail_secret = None
# with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'secrets.json')) as data_file:
# with open(os.path.join(os.path.dirname('C:\\Users\\809452\\gta_swarm'), 'secrets.json')) as data_file:
with open(os.path.join('C:\\Users\\809452\\gta_swarm', 'secrets.json')) as data_file:
sendmail_secret = (json.load(data_file))['sendmail_win']
except FileNotFoundError:
print('Secret file not found..')
return
print('Setting account..')
# Username in WINDOMAIN\username format. Office365 wants usernames in PrimarySMTPAddress
# ('[email protected]') format. UPN format is also supported.
credentials = Credentials(username='APACNB\\809452', password=sendmail_secret['password'])
print('Discovering..')
# If the server doesn't support autodiscover, use a Configuration object to set the server
# location:
config = Configuration(server='emailuk.kuoni.com', credentials=credentials)
try:
account = Account(primary_smtp_address=email, config=config,
autodiscover=False, access_type=DELEGATE)
except ConnectionError as e:
print('Fatal: Connection Error.. aborted..')
return
print('Logged in as: ' + str(email))
to_date = datetime.datetime.now() + datetime.timedelta(days=1)
# from_date = to_date + datetime.timedelta(days=days)
from_date = datetime.datetime.now() + datetime.timedelta(days=days)
# tz = EWSTimeZone.timezone('Europe/Copenhagen')
# tz = EWSTimeZone.timezone('Europe/London')
tz = EWSTimeZone.timezone('Asia/Shanghai')
# tz = EWSTimeZone.localzone()
print('Filtering from ' + str(from_date))
print('Filtering to ' + str(to_date))
items_for_days = account.inbox.filter(datetime_received__range=(
# tz.localize(EWSDateTime(2017, 1, 1)),
tz.localize(EWSDateTime(from_date.year, from_date.month, from_date.day)),
# tz.localize(EWSDateTime(2018, 1, 1))
tz.localize(EWSDateTime(to_date.year, to_date.month, to_date.day))
# )).filter(sender='[email protected]') # Filter by a date range
# )).filter(author='[email protected]') # Filter by a date range
)) # Filter by a date range
# )).filter(sender='[email protected]')
if not items_for_days:
print('items_for_days is none..')
print('Items count: ' + str(items_for_days.count()))
for item in items_for_days:
# print('? Email subject: ' + item.subject)
# print('?? Email sender: ' + str(item.sender))
# print('??? Email date time: ' + str(item.datetime_received))
for attachment in item.attachments:
if isinstance(attachment, FileAttachment):
print('- Email subject: ' + item.subject)
print('-- Email sender: ' + str(item.sender))
print('--- Email date time: ' + str(item.datetime_received))
# local_path = os.path.join('/tmp', attachment.name)
# local_path = os.path.join('', attachment.name)
# CTRIP---API-Errors---API-valuation-step-issues--h-_v205111_s2608_2018-07-01-00-00
if 'CTRIP---API-Errors---API-valuation-step-issues' not in attachment.name:
print('Warning: attachment not API report.. ')
continue
local_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), attachment.name)
with open(local_path, 'wb') as f:
try:
f.write(attachment.content)
except ErrorItemNotFound:
print('Error: item not found.. ')
continue
print('----> Saved attachment to', local_path)
# Test break..
return
# recipient_email = '[email protected]'
# recipient_email1 = '[email protected]'
# recipient_email2 = '[email protected]'
# recipient_email3 = '[email protected]'
# recipient_email4 = '[email protected]'
# recipient_email6 = '[email protected]'
# recipient_email5 = '[email protected]'
# body_text = 'FYI\n' + \
# 'Best\n' + \
# '-Yu'
# title_text = '[[[ Ctrip hotel reference ]]]'
# # Or, if you want a copy in e.g. the 'Sent' folder
# m = Message(
# account=account,
# folder=account.sent,
# sender=Mailbox(email_address=email),
# author=Mailbox(email_address=email),
# subject=title_text,
# body=body_text,
# # to_recipients=[Mailbox(email_address=recipient_email1),
# # Mailbox(email_address=recipient_email2),
# # Mailbox(email_address=recipient_email3)
# # ]
# # to_recipients=[Mailbox(email_address=recipient_email1),
# # Mailbox(email_address=recipient_email2),
# # Mailbox(email_address=recipient_email3),
# # Mailbox(email_address=recipient_email4),
# # Mailbox(email_address=recipient_email5)
# # ]
# to_recipients=[Mailbox(email_address=recipient_email1),
# Mailbox(email_address=recipient_email2),
# Mailbox(email_address=recipient_email3),
# Mailbox(email_address=recipient_email4)
# ]
# )
# with open(newest, 'rb') as f:
# update_csv = FileAttachment(name=newest, content=f.read())
# m.attach(update_csv)
# m.send_and_save()
# print('Message sent.. ')
if __name__ == '__main__':
sendmail_win_hb()