forked from TUW-VieVS/VieSchedpp_AUTO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendMail.py
More file actions
173 lines (139 loc) · 4.78 KB
/
SendMail.py
File metadata and controls
173 lines (139 loc) · 4.78 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
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
import datetime
import glob
import os
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from Helper import Message
def changeSendMailsFlag(flag):
"""
change sendMail flag
:param flag: boolean flag
:return: None
"""
SendMail.flag_sendMail = flag
def delegate_send(slot):
"""
change delegate send() function to different function (e.g.: send via gmail-server or bkg-server)
:param slot: name ("Gmail", "BKG")
:return: None
"""
if slot.lower() == "gmail":
SendMail.send = send_gmail
print("Send mails via [GMAIL]")
elif slot.lower() == "bkg":
SendMail.send = send_bkg
print("Send mails via [BKG]")
else:
Message.addMessage("ERROR: SMTP server slot \"{:}\" not found".format(slot))
def writeMail_upload(code, emails):
"""
write email with upload message
:param code: session code
:param emails: list of email addresses
:return:
"""
body = Message.msg_header + "\n" + \
Message.msg_program + "\n" + \
Message.msg_session + "\n" + \
Message.msg_download + "\n" + \
Message.msg_log
if SendMail.flag_sendMail:
msg = MIMEMultipart()
msg['From'] = "VieSched++ AUTO"
msg['To'] = ", ".join(emails)
today = datetime.date.today()
msg['Subject'] = "[upload] [VieSched++ AUTO] {} ({:%B %d, %Y})".format(code, today)
msg.attach(MIMEText(body))
SendMail.send(msg)
def writeMail(path, emails, body=None):
"""
write an email
:param path: path to "selected" folder
:param emails: list of email addresses
:param body: email body text. If None text will be taken from Message object
:return: None
"""
skdFile = glob.glob(os.path.join(path, "*.skd"))[0]
operationNotesFile = skdFile.replace(".skd", ".txt")
vexFile = skdFile.replace(".skd", ".vex")
figures = glob.glob(os.path.join(path, "*.png"))
if body is None:
body = Message.msg_header + "\n" + \
Message.msg_program + "\n" + \
Message.msg_session + "\n" + \
Message.msg_download + "\n" + \
Message.msg_log
with open(os.path.join(path, "email.txt"), "w") as f:
f.write(body)
if SendMail.flag_sendMail:
msg = MIMEMultipart()
msg['From'] = "VieSched++ AUTO"
msg['To'] = ", ".join(emails)
sessionCode = os.path.basename(os.path.dirname(path))
msg['Subject'] = "[VieSched++ AUTO] {}".format(sessionCode)
msg.attach(MIMEText(body))
for f in [skdFile, operationNotesFile, vexFile, *figures]:
with open(f, "rb") as fil:
part = MIMEApplication(fil.read(), Name=os.path.basename(f))
# After the file is closed
part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(f)
msg.attach(part)
SendMail.send(msg)
def writeErrorMail(to):
"""
write an email in case an error raised
:param to: list of email addresses
:return: None
"""
if SendMail.flag_sendMail:
msg = MIMEMultipart()
msg['From'] = "VieSched++ AUTO"
try:
msg['To'] = ", ".join(to)
except:
msg['To'] = to
today = datetime.date.today()
msg['Subject'] = "[ERROR] [VieSched++ AUTO] {:%B %d, %Y}".format(today)
body = Message.msg_header + "\n" + \
Message.msg_program + "\n" + \
Message.msg_session + "\n" + \
Message.msg_download + "\n" + \
Message.msg_log
msg.attach(MIMEText(body))
SendMail.send(msg)
def send_gmail(message):
"""
send an email message via default gmail server
:param message: email message
:return: None
"""
message['From'] = "VieSched++ AUTO"
if SendMail.flag_sendMail:
print("Send email (Gmail) to: " + message['To'], end="... ")
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login("vieschedpp.auto@gmail.com", "vlbi2000")
server.send_message(message)
server.quit()
print("finished!")
def send_bkg(message):
"""
send an email message via bkg server (for Wettzell)
:param message: email message
:return: None
"""
message['From'] = "vieschedpp.auto@wettzell.de"
if SendMail.flag_sendMail:
print("Send email (BKG) to: " + message['To'], end="... ")
server = smtplib.SMTP('localhost', 25)
server.ehlo()
server.send_message(message)
server.quit()
print("finished!")
def undefined():
print("ERROR: email is undefined!")
class SendMail:
flag_sendMail = True
send = undefined