0

Ok, I have the below code that runs on a Raspberry Pi. issue is that the below code works multiple times when I test but after a day or so it runs ie screen print is displayed and no crash or error but it does not sendmail. I am more a .net programmer so Python is very new to me and wanted to know if there is an obvious bug. or the best way to debug python. thanks in advance any help is greatly appreciated.

# Send an HTML email with an embedded image and a plain text message for
# email clients that don't want to display the HTML.

import datetime
import time
import smtplib
import urllib2
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN, pull_up_down = gpio.PUD_DOWN)


from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
print 'SMTP Doorbell Application loaded @ ' + dtnow

smtpServer = 'smtp.server.com'
smtpPort = '587'

smtpUser = 'smtuser@email.com'
smtpPass = 'P@ssword1'

strFrom = 'fromname@email.com'
strTo = 'toname@email.com'


# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'RPI - Doorbell Alert'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'

header = 'From: ' + strFrom + '\n' + 'To: ' + strTo + '\n' + 'Subject: RPI - Doorbell Alert' 


while True:
    input_value = gpio.input(17)
    if input_value == False:

        print("\033c")
        dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
        print 'Doorbell has been pressed @ ' + dtnow

                # Encapsulate the plain and HTML versions of the message body in an
                # 'alternative' part, so message agents can decide which they want to display.
                msgAlternative = MIMEMultipart('alternative')
                msgRoot.attach(msgAlternative)

                dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
                msgText = MIMEText('Doorbell Rang @ ' + dtnow)
                msgAlternative.attach(msgText)

                # We reference the image in the IMG SRC attribute by the ID we give it below
                msgText = MIMEText('Doorbell Rang @ ' + dtnow + ' <br><img src="cid:image1">', 'html')
                msgAlternative.attach(msgText)


        # Get ReoLink Image
                request = urllib2.Request(
                        r'http://192.168.0.11/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=wuuPhkmUCeI9WG7C&user=admin&password=12345',
                        headers={'User-Agent':'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 firefox/2.0.0.11'})
                page = urllib2.urlopen(request)
                with open('doorbell.png','wb') as f:
                        f.write(page.read())

                # This example assumes the image is in the current directory
                fp = open('doorbell.png', 'rb')
                msgImage = MIMEImage(fp.read())
                fp.close()

                # Define the image's ID as referenced above
                msgImage.add_header('Content-ID', '<image1>')
                msgRoot.attach(msgImage)

                # Send the email (this example assumes SMTP authentication is required)
                smtp = smtplib.SMTP(smtpServer, smtpPort)

                smtp.ehlo()
                smtp.starttls()
                smtp.ehlo()

                smtp.login(smtpUser,smtpPass)
                smtp.sendmail(strFrom, strTo.split(','), msgRoot.as_string())
                time.sleep(15)
                smtp.quit()

            print 'SMS sent successful!'
        print header + '\n' + 'Message: Doorbell Rang @ ' + dtnow

while input_value == False:
            input_value = gpio.input(17)
Jim Brown
  • 21
  • 3

1 Answers1

0

Fixed, I moved the while loop to include the msgRoot

    # Send an HTML email with an embedded image and a plain text message for
    # email clients that don't want to display the HTML.

    import datetime
    import time
    import smtplib
    import urllib2
    import RPi.GPIO as gpio
    gpio.setmode(gpio.BCM)
    gpio.setup(17, gpio.IN, pull_up_down = gpio.PUD_DOWN)


    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText
    from email.MIMEImage import MIMEImage

    dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
    print 'SMTP Doorbell Application loaded @ ' + dtnow

    smtpServer = 'smtp.server.com'
    smtpPort = '587'

    smtpUser = 'smtuser@email.com'
    smtpPass = 'P@ssword1'

    strFrom = 'fromname@email.com'
    strTo = 'toname@email.com'

 while True:
        input_value = gpio.input(17)
        if input_value == False:


    # Create the root message and fill in the from, to, and subject headers
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = 'RPI - Doorbell Alert'
    msgRoot['From'] = strFrom
    msgRoot['To'] = strTo
    msgRoot.preamble = 'This is a multi-part message in MIME format.'

    header = 'From: ' + strFrom + '\n' + 'To: ' + strTo + '\n' + 'Subject: RPI - Doorbell Alert' 


            print("\033c")
            dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
            print 'Doorbell has been pressed @ ' + dtnow

                    # Encapsulate the plain and HTML versions of the message body in an
                    # 'alternative' part, so message agents can decide which they want to display.
                    msgAlternative = MIMEMultipart('alternative')
                    msgRoot.attach(msgAlternative)

                    dtnow = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S")
                    msgText = MIMEText('Doorbell Rang @ ' + dtnow)
                    msgAlternative.attach(msgText)

                    # We reference the image in the IMG SRC attribute by the ID we give it below
                    msgText = MIMEText('Doorbell Rang @ ' + dtnow + ' <br><img src="cid:image1">', 'html')
                    msgAlternative.attach(msgText)


            # Get ReoLink Image
                    request = urllib2.Request(
                            r'http://192.168.0.11/cgi-bin/api.cgi?cmd=Snap&channel=0&rs=wuuPhkmUCeI9WG7C&user=admin&password=12345',
                            headers={'User-Agent':'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 firefox/2.0.0.11'})
                    page = urllib2.urlopen(request)
                    with open('doorbell.png','wb') as f:
                            f.write(page.read())

                    # This example assumes the image is in the current directory
                    fp = open('doorbell.png', 'rb')
                    msgImage = MIMEImage(fp.read())
                    fp.close()

                    # Define the image's ID as referenced above
                    msgImage.add_header('Content-ID', '<image1>')
                    msgRoot.attach(msgImage)

                    # Send the email (this example assumes SMTP authentication is required)
                    smtp = smtplib.SMTP(smtpServer, smtpPort)

                    smtp.ehlo()
                    smtp.starttls()
                    smtp.ehlo()

                    smtp.login(smtpUser,smtpPass)
                    smtp.sendmail(strFrom, strTo.split(','), msgRoot.as_string())
                    time.sleep(15)
                    smtp.quit()

                print 'SMS sent successful!'
            print header + '\n' + 'Message: Doorbell Rang @ ' + dtnow

    while input_value == False:
                input_value = gpio.input(17)
Jim Brown
  • 21
  • 3