Created
December 12, 2024 01:23
-
-
Save AO8/acf694a8f088fb332b3b81ceb08a33d1 to your computer and use it in GitHub Desktop.
Updated: Email a timestamped PiCamera photo with Gmail
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
import os | |
import picamera | |
import smtplib | |
import ssl | |
import time | |
from datetime import datetime as dt | |
from email.mime.image import MIMEImage | |
from email.mime.multipart import MIMEMultipart | |
def take_pic(img_name): | |
with picamera.PiCamera() as camera: | |
camera.rotation = 180 | |
camera.start_preview() | |
camera.annotate_background = picamera.Color('black') | |
camera.annotate_text = dt.now().strftime(' %b %d, %Y %H:%M %p') | |
time.sleep(3) | |
camera.capture(img_name) | |
def send_pic(sender, passcode, receiver, img_file): | |
msg = MIMEMultipart() | |
msg['From'] = sender | |
msg['To'] = receiver | |
img_data = open(img_file, 'rb').read() | |
img = MIMEImage(img_data, name=os.path.basename(img_file)) | |
msg.attach(img) | |
context = ssl.create_default_context() | |
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as s: | |
s.login(sender, passcode) | |
s.sendmail(sender, receiver, msg.as_string()) | |
take_pic(#photo filename) | |
send_pic(#sender's gmail, #sender's gmail app passcode, #receiver's email, #photo filename) | |
# Note, Google has enhanced its security measures. | |
# Using your regular Gmail password to send emails with Python is no longer recommended. | |
# Instead, you need to generate an App Password specifically for this purpose. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment