Checkmark

sendnotify

May 30th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import smtplib
  3. from email.MIMEMultipart import MIMEMultipart
  4. from email.MIMEBase import MIMEBase
  5. from email.MIMEText import MIMEText
  6. from email.Utils import COMMASPACE, formatdate
  7. from email import Encoders
  8. import os
  9. import time
  10.  
  11. USERNAME = "[email protected]"
  12. PASSWORD = "password"
  13.  
  14. def sendMail(to, subject, text, files=[]):
  15.     assert type(to)==list
  16.     assert type(files)==list
  17.  
  18.     msg = MIMEMultipart()
  19.     msg['From'] = USERNAME
  20.     msg['To'] = COMMASPACE.join(to)
  21.     msg['Date'] = formatdate(localtime=True)
  22.     msg['Subject'] = subject
  23.  
  24.     msg.attach( MIMEText(text) )
  25.  
  26.     for file in files:
  27.         part = MIMEBase('application', "octet-stream")
  28.         part.set_payload( open(file,"rb").read() )
  29.         Encoders.encode_base64(part)
  30.         part.add_header('Content-Disposition', 'attachment; filename="%s"'
  31.                        % os.path.basename(file))
  32.         msg.attach(part)
  33.  
  34.     server = smtplib.SMTP('smtp.gmail.com:587')
  35.     server.ehlo_or_helo_if_needed()
  36.     server.starttls()
  37.     server.ehlo_or_helo_if_needed()
  38.     server.login(USERNAME,PASSWORD)
  39.     server.sendmail(USERNAME, to, msg.as_string())
  40.    
  41.        
  42.         print "Mail sent."
  43.        
  44.         server.quit()
  45.        
  46. # end sendMail
  47.  
  48.  
  49.  
  50. localtime = time.asctime(time.localtime(time.time()))
  51.  
  52.  
  53. #  change to recipient's email address
  54.  
  55. sendMail( ["[email protected]"],
  56.         "Doorbell notification",
  57.          localtime +": " + "Someone is ringing the doorbell, picture attached.",
  58.         ["/home/pi/Desktop/Doorbell/webcam.jpg"] )
Advertisement
Add Comment
Please, Sign In to add comment