forked from tdicola/pi-facerec-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailer.py
48 lines (45 loc) · 1.51 KB
/
mailer.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
"""Raspberry Pi Face Recognition Treasure Box
Email Script
Script & implementation by Michael Rosenberg
Project by Tony DiCola
"""
import smtplib
import datetime
import config
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders
# Set now to whatever time it currently is
now = datetime.datetime.now()
def email(filename):
# Define the email subject
SUBJECT = "Unknown intruder on " + str(now.day)+"-"+str(now.month)+"-"+str(now.year)
# Create a variable based on the configuration recipient
RECIPIENT_EMAIL = config.RECIPIENT_EMAIL
# If the recipient field is blank in the config the email should go to the sender
if (RECIPIENT_EMAIL == ''):
RECIPIENT_EMAIL = config.SENDER_EMAIL
# Create the email
msg = MIMEMultipart()
msg['Subject'] = SUBJECT
msg['From'] = config.SENDER_EMAIL
msg['To'] = RECIPIENT_EMAIL
part = MIMEBase('application', "octet-stream")
part.set_payload(open(filename, "rb").read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="'+filename+'"')
msg.attach(part)
# Connect to the email server
server = smtplib.SMTP(config.SMTP_SERVER)
server.ehlo()
server.starttls()
# Log into the sender email address and send the email
try:
server.login(config.SENDER_EMAIL, config.SENDER_PASSWORD)
server.sendmail(config.SENDER_EMAIL, RECIPIENT_EMAIL, msg.as_string())
print "Email Sent!"
except Exception:
print('Login Failed!')
print(traceback.format_exc())
# Close server connection
server.quit