-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendmail_win_at2.py
143 lines (127 loc) · 5.14 KB
/
sendmail_win_at2.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
#!/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('C:\\Users\\809452\\gta_swarm', '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_l2b_weekly_')
@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]'
recipient_email6 = '[email protected]'
body_text = 'FYI\n\n' + \
'Key China clients L2B list. Please note that data for the most current week is not complete. \"Percentage\" column is difference between 4th and 5th week. \n\n' + \
'This is an automated message. Please do not reply.\n\n' + \
'Best\n' + \
'-Yu Leng'
title_text = '[[[ GTA L2B Weekly ]]]'
# 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)
# ]
# 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),
# Mailbox(email_address=recipient_email6)
# ]
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),
Mailbox(email_address=recipient_email6)
]
)
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_hb2()