Last active
September 17, 2018 00:08
-
-
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.
This file contains hidden or 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 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