This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailcomposer.py
executable file
·63 lines (52 loc) · 2.15 KB
/
mailcomposer.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
import smtplib
from email.mime.text import MIMEText
from uzbooking import CLASS_LETTERS_UK
classes = {v:k for k, v in CLASS_LETTERS_UK.items()}
class MailComposer:
serverHostname = 'mail.hpcc.kpi.ua:587'
username = '[email protected]'
password = ''
recipients = []
def __init__(self, server, username, password, recipient):
self._trains = []
self._server = server
self._username = username
self._password = password
self._recipient = recipient
def add_trains(self, monitor, trains):
self._trains.append((monitor, trains))
def _compose_message_body(self):
strlist = []
datetime_format = '%Y.%m.%d %H:%M'
for monitor, trains in self._trains:
strlist.append("%s %s -> %s\n"%(
monitor['departure_date'].strftime('%Y.%m.%d'),
monitor['departure_station_name'],
monitor['destination_station_name']))
for train in trains:
strlist.append('\t%s %s - %s\n'%(
train['number'],
train['departure_station_name'],
train['destination_station_name']))
strlist.append('\t\tDeparture: %s | Arrival: %s\n'%(
train['departure_datetime'].strftime(datetime_format),
train['arrival_datetime'].strftime(datetime_format)))
strlist.append('\t\t')
for k,v in classes.items():
if train['seats'][k]:
strlist.append('%s:%s '%(v,train['seats'][k]))
strlist.append('\n')
return ''.join(strlist)
def send_mail(self):
msg_body = self._compose_message_body()
msg = MIMEText(msg_body)
msg['From'] = self._username
msg['To'] = self._recipient
msg['Subject'] = 'Railroad tickets available'
server = smtplib.SMTP(self._server)
server.starttls()
server.login(self._username, self._password)
server.sendmail(self._username, self._recipient, msg.as_string())
server.quit()
if __name__=='__main__':
pass