-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPorkPiCheckEmail.py
More file actions
153 lines (111 loc) · 4.17 KB
/
PorkPiCheckEmail.py
File metadata and controls
153 lines (111 loc) · 4.17 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
# ######################################################################################
#
# PorkPiCheckEmail.py - Check to see if emailed reboot or shutdown commands
#
# ######################################################################################
import sys
import imaplib
import getpass
import email
import email.header
import datetime
import time
import smtplib
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
EMAIL_ACCOUNT = "your_raspberrypi_email_address@gmail.com"
EMAIL_FOLDER = "Inbox"
PASSWORD = "your_password"
# #######################################################################################
# process_mailbox
# #######################################################################################
def process_mailbox(M):
"""
Do something with emails messages in the folder.
For the sake of this example, print some headers.
"""
rv, data = M.search(None, "ALL")
if rv != 'OK':
print "No messages found!"
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print( "ERROR getting message", num)
return
msg = email.message_from_string(data[0][1])
subject = msg['Subject']
sender = msg['From']
if sender == 'sender name <sender_name@gmail.com>' and subject =='reboot':
send_email("Rebooted by email")
cleanup_mailbox_and_logout(M)
reboot()
if sender == 'sender name <sender_name@gmail.com>' and subject =='shutdown':
send_email("Shutdown by email")
cleanup_mailbox_and_logout(M)
shutdown()
# #######################################################################################
# cleanup_mailbox_and_logout
# #######################################################################################
def cleanup_mailbox_and_logout(M):
typ, data = M.search(None, 'ALL')
for num in data[0].split():
M.store(num, '+FLAGS', '\\Deleted')
M.expunge()
M.close()
M.logout()
# #######################################################################################
# send_email
# #######################################################################################
def send_email(send_subject):
fromaddr = "your_raspberrypi_email_address@gmail.com"
toaddr = "your_email address"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = send_subject
body = send_subject
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, PASSWORD)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
# #######################################################################################
# Reboot
# #######################################################################################
def reboot():
command = "/usr/bin/sudo /sbin/shutdown -r now"
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print output
# #######################################################################################
# shutdown
# #######################################################################################
def shutdown():
command = "/usr/bin/sudo /sbin/shutdown now"
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print output
# #######################################################################################
# Main loop
# #######################################################################################
# login to gmail account
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
rv, data = M.login(EMAIL_ACCOUNT, PASSWORD)
except imaplib.IMAP4.error:
print "LOGIN FAILED!!! "
sys.exit(1)
# rocess inbox
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print "\tCheck email for reboot/shutdown"
process_mailbox(M)
else:
print "ERROR: Unable to open mailbox ", rv
M.logout()