Python RPI GPIO:SMTP代码捕获HTTP图像保存和发送

时间:2017-03-01 23:25:52

标签: python raspberry-pi gpio smtplib

好的,我有以下代码在Raspberry Pi上运行。问题是,当我测试时,下面的代码可以多次运行,但是运行一天左右后,即显示屏幕打印并且没有崩溃或错误,但它不会发送邮件。我更像是一个.net程序员,所以Python对我来说很新,想知道是否有明显的bug。或者调试python的最佳方法。在此先感谢任何帮助非常感谢。

listing[0]

1 个答案:

答案 0 :(得分:0)

修复了,我移动了while循环以包含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)