-
Notifications
You must be signed in to change notification settings - Fork 0
/
flogger_email_log.py
81 lines (75 loc) · 2.82 KB
/
flogger_email_log.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
import smtplib
import base64
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
from __builtin__ import file
import os
import datetime
def email_log2(sender, receiver, filepath, date, settings):
# print "email_log2 with sender=%s, receiver=%s, filepath=%s" % (sender, receiver, filepath)
FLIGHT_LOG_FILE = "flight_log.csv"
# filepath = path + file
fpr = open(filepath, "r")
fpw = open(FLIGHT_LOG_FILE, "w+") # Read and write
data_lines = fpr.read()
line_list = data_lines.split("\n")
# print "Line_list is: ", line_list
for aline in line_list:
if aline == "":
print "BREAK end of flights.csv"
break
# print "Aline is: ", aline
pl = aline.split(",")
spl = pl[0:8]
# print "Pl is: ", pl
npl = "%s,%s,%s,%s,%s,%s,%s,%s\n" % (str(pl[0]), str(pl[1]), str(pl[2]), str(pl[3]), str(pl[4]), str(pl[5]), str(pl[6]), str(pl[7]))
# print "npl is: ", npl
fpw.write(npl)
# return
fpw.close()
fromaddr = sender
toaddr = receiver
print "From: " + fromaddr + " To: " + toaddr
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Flight log for today: %s from %s" % (date, settings.APRS_USER)
if os.path.getsize(filepath) == 0:
print "No file to attach"
body = "No flights for today: %s" % date
msg.attach(MIMEText(body, 'plain'))
else:
# There is a flight log for today so attach it
print "Attach file"
body = "Flight log for : %s" % date
msg.attach(MIMEText(body, 'plain'))
attachment = open(FLIGHT_LOG_FILE, "r+")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % FLIGHT_LOG_FILE)
msg.attach(part)
# print "SMTP URL: " + str(settings.FLOGGER_SMTP_SERVER_URL) + " SMTP Port: " + str(settings.FLOGGER_SMTP_SERVER_PORT)
try:
server = smtplib.SMTP(settings.FLOGGER_SMTP_SERVER_URL, settings.FLOGGER_SMTP_SERVER_PORT)
# server = smtplib.SMTP(settings.FLOGGER_SMTP_SERVER_URL, int(settings.FLOGGER_SMTP_SERVER_PORT))
# print "SMTP Server connection ok"
except:
print "SMTP Server connection failed"
text = msg.as_string()
print "Msg string is: ", text
try:
server.sendmail(fromaddr, toaddr, text)
# print "Sendmail ok"
except:
print "Sendmail failed"
server.quit()
fpw.close()
try:
os.remove(FLIGHT_LOG_FILE)
except Exception, e:
print "Remove FLIGHT_LOG_FILE failed, reason: ", e
return