I'm using motion, a webcam and a simple python script to create a security system that will email me the latest photo when motion is detected. Main problem is I can't get the program to send me the latest photo (it's sending me the same photo every time). I have to set the 2 parameters which are text for the body of email and file to attach as per below. What can I add to my script to dispense with the need for adding a second parameter so it will always just email the latest jpeg.
python sendemailattach.py 'body of text', '/home/pi/webcam/photo.jpg'
Code: Select all
#!/usr/bin/env python """ Home Automation: sends an e-mail with a photograph attachment For the Raspberry Pi """ import sys import smtplib from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart def emailphoto(msgtext, afilename): #enter the e-mail account username between the quotes smtp_user = "[email protected]" #enter the e-mail account password between the quotes smtp_pass = "sunders2006" msg = MIMEMultipart() #enter the target e-mail address between the quotes msg['To'] = "[email protected]" #enter the e-mail account username between the quotes msg['From'] = "Valdo" #enter the message subject between the quotes msg['Subject'] = "Hello" #That is what you see if don't have an e-mail reader: msg.preamble = 'Multipart message.\n' #sys.argv is the 1st parameter that is passed to this #and it contains the text for the body of the e-mail part = MIMEText(msgtext) msg.attach(part) #The next 3 lines attach the photo using the filename #passed in as the second parameter to this program part = MIMEApplication(open(afilename,"rb").read()) part.add_header('Content-Disposition', 'attachment', filename=afilename) msg.attach(part) #enter the SMPT server URL or IP Address between the quotes s = smtplib.SMTP_SSL("smtp.gmail.com", 465) s.login(smtp_user,smtp_pass) s.sendmail(msg['From'], msg['To'], msg.as_string()) s.quit() def main(): emailphoto(sys.argv, sys.argv) if __name__ == "__main__": main()