Created
January 12, 2013 01:16
-
-
Save hamstu/4515479 to your computer and use it in GitHub Desktop.
Email a photo/file from your computer, with Python! Usage: emailer.py [file] Made for: http://www.reddit.com/r/raspberry_pi/comments/16esrj/how_do_i_send_email_on_motion_trigger/
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/python | |
""" | |
emailer.py | Script to send attachments via a Gmail account | |
- Based on http://codecomments.wordpress.com/2008/01/04/python-gmail-smtp-example/ | |
- Fixed small bugs and cleaned up the formatting, etc. | |
USAGE: | |
emailer.py [file1, file2, ...] | |
OPTIONS: | |
All the configuration settings are in the file, read on. | |
@author Hamish Macpherson | |
@url http://hami.sh/ | |
""" | |
import os, sys | |
import smtplib | |
import mimetypes | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEBase import MIMEBase | |
from email.MIMEText import MIMEText | |
from email.MIMEAudio import MIMEAudio | |
from email.MIMEImage import MIMEImage | |
from email.Encoders import encode_base64 | |
# CONSTANTS | |
SUBJECT = 'New Image Captured' | |
RECIPIENT = '[email protected]' | |
MESSAGE = 'See attachment.' | |
GMAIL_USER = '[email protected]' | |
GMAIL_PASSWORD = '' # FILL ME IN | |
def sendMail(subject, text, attachmentFilePaths): | |
msg = MIMEMultipart() | |
msg['From'] = GMAIL_USER | |
msg['To'] = RECIPIENT | |
msg['Subject'] = subject | |
msg.attach(MIMEText(text)) | |
for attachmentFilePath in attachmentFilePaths: | |
msg.attach(getAttachment(attachmentFilePath)) | |
mailServer = smtplib.SMTP('smtp.gmail.com', 587) | |
mailServer.ehlo() | |
mailServer.starttls() | |
mailServer.ehlo() | |
mailServer.login(GMAIL_USER, GMAIL_PASSWORD) | |
mailServer.sendmail(GMAIL_USER, RECIPIENT, msg.as_string()) | |
mailServer.close() | |
print('Sent email to %s' % RECIPIENT) | |
def getAttachment(attachmentFilePath): | |
contentType, encoding = mimetypes.guess_type(attachmentFilePath) | |
if contentType is None or encoding is not None: | |
contentType = 'application/octet-stream' | |
mainType, subType = contentType.split('/', 1) | |
file = open(attachmentFilePath, 'rb') | |
if mainType == 'text': | |
attachment = MIMEText(file.read()) | |
elif mainType == 'message': | |
attachment = email.message_from_file(file) | |
elif mainType == 'image': | |
attachment = MIMEImage(file.read(),_subType=subType) | |
elif mainType == 'audio': | |
attachment = MIMEAudio(file.read(),_subType=subType) | |
else: | |
attachment = MIMEBase(mainType, subType) | |
attachment.set_payload(file.read()) | |
encode_base64(attachment) | |
file.close() | |
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(attachmentFilePath)) | |
return attachment | |
if __name__ == '__main__': | |
paths = sys.argv[1:] | |
sendMail(SUBJECT, MESSAGE, paths) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment