Send email a webcam captured png
Posted by SGezerlis on October Sun 9th 10:59 AM - Never Expires| View : 150Syntax: PYTHON
1849 characters | 50 lines | 1.81 KB
- #Author: Spiros Gezerlis - gezerlis_gmail_com
- import smtplib
- from email.mime.image import MIMEImage
- from email.mime.multipart import MIMEMultipart
- from time import strftime
- import commands
- import time
- #Setting variables
- INTERVAL = 60
- WEBCAM_IMAGE = "/path/to/file.png"
- WEBCAM_RESOLUTION = "640x480" #1280x1024 1024x768 640x480
- WEBCAM_CMD = "fswebcam -r "+WEBCAM_RESOLUTION+" "+WEBCAM_IMAGE
- EMAIL_SUBJECT = 'Screenshot @ '+strftime("%Y-%m-%d %H:%M:%S") +" ("+WEBCAM_RESOLUTION+")"
- EMAIL_SENDER = "[email protected]"
- LINE = "=============="
- COMMASPACE = ', '
- while (1):
- #Taking screenshot
- print LINE
- print "Starting "+EMAIL_SUBJECT+" to be sent via email"
- commands.getstatusoutput(WEBCAM_CMD)
- print "Webcam: "+WEBCAM_CMD
- #Constucting email
- print "Constructing email"
- msg = MIMEMultipart()
- msg['Subject'] = EMAIL_SUBJECT
- msg['From'] = EMAIL_SENDER
- msg['To'] = COMMASPACE.join(EMAIL_RECIPIENT_LIST)
- #Opening and Attaching 'image' file to email
- print "Attaching "+WEBCAM_IMAGE+" to email"
- fp = open(WEBCAM_IMAGE, 'rb')
- img = MIMEImage(fp.read())
- fp.close()
- msg.attach(img)
- # Send the email via linux's own SMTP server and sleep for 'interval' seconds
- print "Sending email @ ",strftime("%Y-%m-%d %H:%M:%S")
- s = smtplib.SMTP('localhost')
- s.sendmail(EMAIL_SENDER, EMAIL_RECIPIENT_LIST, msg.as_string())
- s.quit()
- print "Email sent @ ",strftime("%Y-%m-%d %H:%M:%S")
- print "Sleeping for "+str(INTERVAL)+" secs"
- print LINE
- print
- time.sleep(INTERVAL)
