Skip to content

Instantly share code, notes, and snippets.

@AO8
Last active September 17, 2018 00:08
Show Gist options
  • Select an option

  • Save AO8/fdfb7908a9bb16ae20b808406eee36bd to your computer and use it in GitHub Desktop.

Select an option

Save AO8/fdfb7908a9bb16ae20b808406eee36bd to your computer and use it in GitHub Desktop.
Simple Python script to attach & email images, handy for sending photos taken with a PiCamera / Raspberry Pi.
# enable less secure apps in gmail before sending
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
# gather inputs
from_addr = input("FROM ADDRESS: ")
pwd = input("PASSWORD: ")
to_addr = input("TO ADDRESS: ")
subject = input("SUBJECT LINE: ")
file = input("PHOTO FILENAME (if not in local directory, use absolute filename): ")
# prepare email
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = from_addr
msg["To"] = to_addr
# attach photo
photo_data = open(file, "rb").read()
photo = MIMEImage(photo_data, name=os.path.basename("photo.jpg"))
msg.attach(photo)
# send email
s = smtplib.SMTP("smtp.gmail.com:587")
s.ehlo()
s.starttls()
s.login(from_addr, pwd)
s.sendmail(from_addr, to_addr, msg.as_string())
s.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment