troubleshooting Question

Receiving Multiple Messages, only need one

Avatar of dynamicOne
dynamicOne asked on
ProgrammingPythonProgramming Theory
8 Comments1 Solution512 ViewsLast Modified:
I downloaded a Python script so I could receive an email when my garage door is open or closed. However, when I run the Python script I receive numerous emails instead of just one. I would only like to receive one email per condition. Eventually I would like to run this script all of the time on my Pi, but I can't until I fix this error. I opened up the garage.txt file and there is nothing in it. Could this be related to the non-stop emails?

Thanks for the help.

Here is the code:

#! /usr/bin/env python
import commands
import smtplib
from email.MIMEText import MIMEText

ser = 'gpio read 1'  #Change /dev/ttyACM0 to your com port

GMAIL_LOGIN = '***********@gmail.com'
GMAIL_PASSWORD = '*********'
SEND_TO = '**********@txt.att.net'
 
def send_email(subject, message, from_addr=GMAIL_LOGIN, to_addr=SEND_TO):
    msg = MIMEText(message)
    msg['Subject'] = subject
    msg['From'] = from_addr
    msg['To'] = to_addr
 
    server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(GMAIL_LOGIN,GMAIL_PASSWORD)
    server.sendmail(from_addr, to_addr, msg.as_string())
    server.close()


while 1: #loop forever
   result = commands.getoutput(ser)
   if result.strip() == "1":
    status=open('garage.txt', 'w')
    status.write("Garage door is open")
    send_email('OPEN', 'The garage door is open')
    status.close()
#    print("open")

   elif result.strip() == "0":
    status=open('garage.txt', 'w')
    status.write("Garage door is closed")
    send_email('CLOSED','The garage door is closed')
    status.close()
#    print("closed")
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros