简体   繁体   中英

making a python program to take a single photo and send it through gmail

A while back i learned all about sending emails in python, and recently i have been learning to take pictures... I was wondering is there a way i could make a program to take a single photo and send it to a email account.

camera = cv2.VideoCapture(0)

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

del(camera)
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()

I got it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM