对于Raspberry Pi 3,我运行了以下程序,旨在每隔2分钟将相机捕获的新图像发送到Gmail帐户。但是出现了问题。在第一次传输中,发送捕获的图像。在第二次传输中,发送第一图像和新拍摄的图像。在第三次传输中,发送了三个图像。我是Python的新手。有人可以提供一些线索吗?非常感谢您的帮助。
import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email import encoders
from picamera import PiCamera
camera = PiCamera()
camera.rotation = 180
fromaddr = "jsmith05@yahoo.com.au"
toaddr = "jsmith05@gmail.com"
msg1 = MIMEMultipart()
msg1['From'] = fromaddr
msg1['To'] = toaddr
msg1['Subject'] = "Screenshot of room"
while True:
camera.start_preview(alpha=200)
time.sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()
filename = "room.jpg"
attachment = open('../Desktop/image.jpg', "r")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg1.attach(part)
text = msg1.as_string()
myserver = smtplib.SMTP_SSL('smtp.mail.yahoo.com', 465)
myserver.login("jsmith05@yahoo.com.au", "Ch53644")
myserver.sendmail(fromaddr, toaddr, text)
myserver.close()
time.sleep(120)
答案 0 :(得分:1)
事实上,您正在一遍又一遍地发送包含更多内容的相同消息。移动它:
msg1 = MIMEMultipart()
msg1['From'] = fromaddr
msg1['To'] = toaddr
msg1['Subject'] = "Screenshot of room"
在while
区块,你应该没问题