def sendNewMail(): #define your personal information below or in a separate file as I did #user_email ="" #passw = "" #sendTo = "" currentTime = datetime.now().time() currentTime = currentTime.replace(microsecond=0) sub = "New alarm" image = "image.jpg" message = MIMEMultipart() message["Subject"] = sub message["From"] = user_email message["To"] = sendTo message.premble = "alert" message.attach( MIMEText("Alarm took place at: " + str(currentTime), "plain")) # If camera won't work for any reason, send email without the image if (os.path.isfile(image)): fp = open(image, "rb") image = MIMEImage(fp.read()) fp.close() message.attach(image) sendIt = smtplib.SMTP("smtp.gmail.com", 587) sendIt.ehlo() sendIt.starttls() sendIt.ehlo() sendIt.login(user_email, passw) sendIt.send_message(message) sendIt.quit() # remove image after sending it so it will not be send again if camera # cannot take a new screenshot try: remove("image.jpg") except OSError: pass
def main(): camera = picamera.PiCamera() camera.brightness = 70 camera.hflip = True camera.vflip = True camera.resolution = (1024, 768) cnt = 0 COMMASPACE = ', ' add = ['LIST1@MAILADDRESS, LIST2@MAILADDRESS'] me = 'ME@MAILADDRESS' smtp_server = 'smtp.gmail.com' port = 465 passwd = 'PASSWORD' while True: GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.IN) inputValue1 = GPIO.input(4) # GPIO.setmode(GPIO.BCM) # GPIO.setup(18, GPIO.IN) # inputValue2 = GPIO.input(18) inputValue2 = False stream = io.BytesIO() if (inputValue1 == True or inputValue2 == True): # pic 1 file = 'smp01/smp' + str(datetime.datetime.today().strftime( "%Y-%m-%d_%H-%M-%S")) + '.jpeg' print(file) camera.start_preview() camera.capture(file) fp = open(file, 'rb') img = fp.read() mj = MIMEImage(img, 'jpeg', filename=file) fp.close() # pic 2 file = 'smp01/smp' + str(datetime.datetime.today().strftime( "%Y-%m-%d_%H-%M-%S")) + '.jpeg' print(file) camera.start_preview() camera.capture(file) fp = open(file, 'rb') img = fp.read() mj = MIMEImage(img, 'jpeg', filename=file) msg = MIMEMultipart() msg['Subject'] = ("Test : Bear apperarance information") msg['To'] = COMMASPACE.join(add) msg.premble = 'Test mail' msg.attach(mj) fp.close() # Send e-mail s = SMTP_SSL(smtp_server, port) s.login(me, passwd) s.sendmail(me, add, msg.as_string()) s.close() print('email send') else: print( str(cnt).zfill(4) + ' : ' + str(inputValue1) + ',' + str(inputValue2)) time.sleep(1) cnt += 1