Last active
June 23, 2017 20:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"] = "[email protected]" | |
msg["To"] = "[email protected]" | |
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("[email protected]","[email protected]", msg.as_string()) | |
s.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment