Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement