Advertisement
Guest User

Untitled

a guest
May 25th, 2017
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import json
  2. import smtplib
  3. import uuid
  4. import os
  5. import glob
  6.  
  7. from os.path import basename
  8. from email.mime.application import MIMEApplication
  9. from email.mime.multipart import MIMEMultipart
  10. from email.mime.text import MIMEText
  11. from email.utils import COMMASPACE, formatdate
  12.  
  13. def send_email(conf):
  14. """
  15. Using the defined mail address on gmail, it send a alert mail with attached images
  16. """"
  17. fromaddr = "address@gmail.com"
  18. for email_address in conf['email_address']:
  19. toaddrs = email_address
  20. print("[INFO] Emailing to {}".format(email_address))
  21. text = 'Hey Someone in Your House!!!!'
  22. subject = 'Security Alert!!'
  23. message = 'Subject: {}\n\n{}'.format(subject, text)
  24.  
  25. msg = MIMEMultipart()
  26. msg['From'] = fromaddr
  27. msg['To'] = toaddrs
  28. msg['Date'] = formatdate(localtime=True)
  29. msg['Subject'] = subject
  30. msg.attach(MIMEText(text))
  31.  
  32. # Taken frames are kept in /tmp folder with concecutive numbering.
  33. files = glob.glob("/tmp/talkingraspi*")
  34. print("[INFO] Number of images attached to email: {}".format(len(files)))
  35. for f in files:
  36. with open(f, "rb") as fil:
  37. part = MIMEApplication(
  38. fil.read(),
  39. Name=basename(f)
  40. )
  41. part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
  42. msg.attach(part)
  43.  
  44. # Credentials (if needed)
  45. username = "gmail_username"
  46. password = "password"
  47. # The actual mail send
  48. server = smtplib.SMTP('smtp.gmail.com:587')
  49. server.starttls()
  50. server.login(username,password)
  51. server.sendmail(fromaddr, toaddrs, msg.as_string())
  52. server.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement