Created
June 8, 2014 17:44
-
-
Save fishkingsin/c9ab2e15489daf1872f3 to your computer and use it in GitHub Desktop.
TakePhotoSendGamil
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
#!/usr/bin/env python | |
import smtplib | |
import picamera | |
import time | |
import datetime | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEBase import MIMEBase | |
from email.MIMEText import MIMEText | |
from email.Utils import COMMASPACE, formatdate | |
from email import Encoders | |
import os | |
folderPath='./photos/' | |
ts = time.time() | |
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d-%H-%M-%S') | |
imgName = folderPath+st+'.jpg' | |
with picamera.PiCamera() as camera: | |
print 'Taking Picture' | |
# camera.start_preview() | |
# time.sleep(5) | |
# camera.capture(imgName) | |
# camera.stop_preview() | |
USERNAME = "your_email" | |
PASSWORD = "your_email_password" | |
def sendMail(to, subject, text, files=[]): | |
assert type(to)==list | |
assert type(files)==list | |
msg = MIMEMultipart() | |
msg['From'] = USERNAME | |
msg['To'] = COMMASPACE.join(to) | |
msg['Date'] = formatdate(localtime=True) | |
msg['Subject'] = subject | |
msg.attach( MIMEText(text) ) | |
for file in files: | |
part = MIMEBase('application', "octet-stream") | |
part.set_payload( open(file,"rb").read() ) | |
Encoders.encode_base64(part) | |
part.add_header('Content-Disposition', 'attachment; filename="%s"' | |
% os.path.basename(file)) | |
msg.attach(part) | |
server = smtplib.SMTP('smtp.gmail.com:587') | |
server.ehlo_or_helo_if_needed() | |
server.starttls() | |
server.ehlo_or_helo_if_needed() | |
server.login(USERNAME,PASSWORD) | |
server.sendmail(USERNAME, to, msg.as_string()) | |
server.quit() | |
sendMail( ["mailto@gmail.com"], | |
"RPi Timelapse", | |
"this is the body text of the email", | |
[imgName] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment