Last active
January 31, 2019 19:30
-
-
Save erogol/3e85678c6d393acd788b1a93fb46f02a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
def send_email(conf): | |
""" | |
Using the defined mail address on gmail, it send a alert mail with attached images | |
"""" | |
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)) | |
# Taken frames are kept in /tmp folder with concecutive numbering. | |
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) | |
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment