forked from neurobionics/neurobionicspi
-
Notifications
You must be signed in to change notification settings - Fork 48
/
startup_mailer.Pifile
157 lines (125 loc) · 4.51 KB
/
startup_mailer.Pifile
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
# Writes the startup_mailer script to the RPi.
RUN tee /etc/startup_mailer.py << EOF
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
from email.message import EmailMessage
import datetime
import os
import secrets
import time
import sys
def initialize_smtp(gmail_user, gmail_password):
"""
Initializes an SMTP server with the provided login credentials.
Parameters:
-----------
gmail_user: Email address of the mailer
gmail_password: Passphrase for mailer's email
Returns: smtlib.SMTP()
"""
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
return smtpserver
def compose_email(gmail_user, recipients, ipaddr, mac, token, today, hostname):
"""
Composes an e-mail with html formatting.
Parameters:
-----------
gmail_user: Email address of mailer
recipients: Email address(es) of recipient(s)
ipaddr: IP Address of the device
mac: MAC Address of the device
token: Unique token generated for each session
today: datetime formatted present date and time.
Returns: EmailMessage()
"""
msg = EmailMessage()
msg['Subject'] = 'Session created for RPi :: %s' % today.strftime('%b %d %Y')
msg['From'] = gmail_user
msg['To'] = ",".join(str(x) for x in recipients)
msg.set_content('''
<!DOCTYPE html>
<html>
<body>
<div style="padding:5px 20px;">
<h1 style="font-size:20px;">Wifi IP Address Emailer Utility v2.0 <br>© University of Michigan Neurobionics Lab</h1>
<p style="font-size:15px;">
<b>Hostname:</b> {5} <br>
<b>Team Name:</b> --- <br>
<b>IP Address:</b> {0} <br>
<b>Unique Identifier:</b> {1}<br>
<b>MAC Address:</b> {2}<br>
<b>Date:</b> {3}<br>
<b>Time:</b> {4}<br>
</p>
<div style="height: 500px;width:400px">
<img src="https://robotics.umich.edu/wp-content/uploads/2018/11/MRobotics_informal_outlines_digital.png" style="width: 230; height: 47px;">
</div>
</div>
</body>
</html>
'''.format(ipaddr, token, mac, today.strftime('%d %b %Y'), today.strftime('%I:%M:%S %p'), hostname), subtype='html')
return msg
def send_email(smtpserver, msg):
"""
Sends an email
Parameters:
-----------
smtpserver: smtlib.SMTP()
msg: EmailMessage()
"""
smtpserver.send_message(msg)
smtpserver.quit()
if __name__ == '__main__':
# Modify recipients here!
recipient_str = "${email}"
recipients = recipient_str.split(", ")
#######################################################
# Initializing an SMTP server with mailer credentials
gmail_user = '[email protected]'
gmail_password = 'oIxS7gniIEDejPwa'
try:
smtpserver = initialize_smtp(gmail_user, gmail_password)
print("Initialized SMTP server.")
except smtplib.SMTPAuthenticationError:
print("Authentication error! Kindly retry after a few minutes. If the issue persists, please contact Dr. Elliott Rouse ([email protected]).")
sys.exit(0)
except socket.gaierror:
print("Unable to retrieve IP address of the host, waiting for 10 seconds.")
time.sleep(10)
try:
smtpserver = initialize_smtp(gmail_user, gmail_password)
print("Initialized SMTP server.")
except socket.gaierror:
print("No internet connectivity. Kindly verify your network configuration (/etc/wpa_supplicant/wpa_supplicant-wlan0.conf).")
sys.exit(0)
#######################################################
# Retrieving information from the host
utctoday = datetime.datetime.utcnow() #UTC
utc2est = datetime.timedelta(hours=5)
today = utctoday - utc2est #EST
hostname = socket.gethostname()
# Get MAC address - individual ID for each device
str_temp = open('/sys/class/net/wlan0/address').read()
mac = str_temp[0:17]
arg = 'ip route list'
p = subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data = p.communicate()
temp = str(data[0])
split_data = temp.split()
ipaddr = split_data[split_data.index('src')+1]
#Generates an unique token for each session
token = secrets.token_urlsafe(16)
#######################################################
# Compose and send an e-mail
msg = compose_email(gmail_user, recipients, ipaddr, mac, token, today, hostname)
send_email(smtpserver, msg)
print("A ticket for the current session has been mailed to the recipient(s).")
EOF