-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendmail_win_hb2.py
128 lines (112 loc) · 4.54 KB
/
sendmail_win_hb2.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
#!/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
sendmail_secret = None
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'secrets.json')) as data_file:
sendmail_secret = (json.load(data_file))['sendmail_win']
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('--filename', default='output_hb_pa_stats_')
@click.option('--email', default='[email protected]')
def sendmail_win_hb2(filename, email):
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
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))
recipient_email = '[email protected]'
recipient_email1 = '[email protected]'
recipient_email2 = '[email protected]'
recipient_email3 = '[email protected]'
recipient_email4 = '[email protected]'
recipient_email5 = '[email protected]'
body_text = 'FYI\n\n' + \
'For chain offline recommendation, please open the attachment and filter \"recommend_offline\" column by \"yes\".\n\n' + \
'This is an automated message. Please do not reply.\n\n' + \
'Best\n' + \
'-Yu Leng'
title_text = '[[[ Hotelbeds APItude PA Stats ]]]'
# 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_email),
Mailbox(email_address=recipient_email2),
Mailbox(email_address=recipient_email3),
Mailbox(email_address=recipient_email4)
]
# to_recipients=[Mailbox(email_address=recipient_email1),
# Mailbox(email_address=recipient_email),
# Mailbox(email_address=recipient_email2),
# Mailbox(email_address=recipient_email3),
# Mailbox(email_address=recipient_email4),
# Mailbox(email_address=recipient_email5)
# ]
)
with open(newest, 'rb') as f:
update_csv = FileAttachment(name=newest, content=f.read())
m.attach(update_csv)
m.send_and_save()
# pu rin to
print('Message sent.. ')
if __name__ == '__main__':
sendmail_win_hb2()