-
Notifications
You must be signed in to change notification settings - Fork 50
/
utils.py
82 lines (64 loc) · 2.25 KB
/
utils.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
79
80
81
82
import json
import smtplib
import uuid
import os
import glob
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
class TempImage:
def __init__(self, basePath="./", ext=".jpg"):
# construct the file path
self.path = "{base_path}/{rand}{ext}".format(base_path=basePath,
rand=str(uuid.uuid4()), ext=ext)
def cleanup(self):
# remove the file
os.remove(self.path)
def send_email(conf):
fromaddr = "address@gmail.com"
for email_address in conf['email_address']:
toaddrs = email_address
print("[INFO] Emailing to {}".format(email_address))
text = 'Hey Someone in Your House!!!!'
subject = 'Security Alert!!'
message = 'Subject: {}\n\n{}'.format(subject, text)
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddrs
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
# set attachments
files = glob.glob("/tmp/talkingraspi*")
print("[INFO] Number of images attached to email: {}".format(len(files)))
for f in files:
with open(f, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=basename(f)
)
part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
msg.attach(part)
# Credentials (if needed) : EDIT THIS
username = "gmail_username"
password = "password"
# The actual mail send
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg.as_string())
server.quit()
def send_mail(conf, files=None,
):
assert isinstance(send_to, list)
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = COMMASPACE.join(send_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
smtp = smtplib.SMTP(server)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()