Skip to content

Instantly share code, notes, and snippets.

@AO8
Last active June 23, 2017 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AO8/c32cc24a517feefd0ba9d91186e239c6 to your computer and use it in GitHub Desktop.
Save AO8/c32cc24a517feefd0ba9d91186e239c6 to your computer and use it in GitHub Desktop.
Take a timestamped photo with PiCamera, then attach and send from Gmail with Python
# Enable less secure apps in Gmail
import os
import smtplib
import email
import sys
import picamera
import time
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
with picamera.PiCamera() as camera:
camera.rotation = 180 # delete this or adjust .rotation to 90, 180, or 270 accordingly
camera.resolution = (1024, 768) # adjust resolution accordingly
camera.start_preview()
time.sleep(2) # camera warm-up
camera.capture("photo.jpg")
f_time = datetime.now().strftime("%A %B %d %Y @ %H:%M")
img_data = open("photo.jpg", "rb").read()
msg = MIMEMultipart()
msg["Subject"] = f_time
msg["From"] = "yourAddress@gmail.com"
msg["To"] = "toAddress@mail.com"
text = MIMEText("Here's my latest photo!")
msg.attach(text)
image = MIMEImage(img_data, name=os.path.basename("photo.jpg"))
msg.attach(image)
s = smtplib.SMTP("smtp.gmail.com:587")
s.starttls()
s.login("yourGmailLogin","yourPassword")
s.sendmail("yourAddress@gmail.com","toAddress@mail.com", msg.as_string())
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment