制作一个python程序来拍摄一张照片并通过gmail发送

时间:2019-01-19 04:11:40

标签: python python-3.x

前一段时间,我了解了有关使用python发送电子邮件的所有知识,最近我一直在学习拍照... 我想知道是否可以制作一个程序来拍摄一张照片并将其发送到电子邮件帐户。

camera = cv2.VideoCapture(0)

for i in range(1):
   return_value, image = camera.read()
   cv2.imwrite('puppy'+str(i)+'.png', image)

del(camera)

1 个答案:

答案 0 :(得分:0)

import smtplib

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email import encoders

import cv2

import time


camera = cv2.VideoCapture(0)
for i in range(1):
return_value, image = camera.read()
cv2.imwrite('puppy'+str(i)+'.png', image)
del(camera)

time.sleep(8)

fromaddr = ""
toaddr = ""

msg = MIMEMultipart()

msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = ""

body = "Test mail"

msg.attach(MIMEText(body, 'plain'))

filename = "puppy0.png"
attachment = open("puppy0.png","rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "PASSWORD OF GMAIL")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

我明白了...