我使用网络摄像头捕获图像,当我按下按钮时它发送邮件。我使用while循环,如下面的代码所示,这个代码运行完美但问题是这个程序发送了这么多图像(也发送早期的图像)我们停止程序运行,所以只有一个邮件附加了很多图像。现在我希望程序在树莓派中连续运行但是它应该只是邮件最后捕获的图像所以我必须做的更改呢?
提前致谢。
这是我的代码:
import smtplib
import time
import subprocess
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import RPi.GPIO as GPIO
# Define these once; use them twice!
strFrom = 'example@gmail.com'
strTo = 'example@gmail.com'
#create email
# Create the root message and fill in the from, to, and subj$
msgRoot = MIMEMultipart()
msgRoot['Subject'] = 'subject'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)
print "press button to send email"
while True:
input=GPIO.input(4)
if input == True:
print "button pressed"
subprocess.Popen(["fswebcam","-r 640x480", "capture.jpg"])
time.sleep(2)
# This example assumes the image is in the current
# directory
fp = open('capture.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgRoot.attach(msgImage)
# send mail
s = smtplib.SMTP('smtp.gmail.com',587)
s.starttls()
s.login('example@gmail.com' , 'password')
s.sendmail(strFrom, strTo,msgRoot.as_string())
s.close()
print "Email sent"
time.sleep(0.2)
答案 0 :(得分:0)
msgRoot.attach(msgImage)
msgRoot.set_payload(msImage) #Such modified
因为你使用的附件相同的副本已经叠加在上升