Skip to content

Instantly share code, notes, and snippets.

@ot
Created December 28, 2010 13:54
Show Gist options
  • Save ot/757221 to your computer and use it in GitHub Desktop.
Save ot/757221 to your computer and use it in GitHub Desktop.
Send solutions for Facebook puzzles (for gmail accounts, but can be easily adapted)
#!/usr/bin/env python
import sys
import getpass
import logging
import os
from contextlib import contextmanager
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 smtplib
TOADDRESS = '1051962371@fb.com'
SMTPSERVER = 'smtp.gmail.com:587'
EMAILDOMAIN = '@gmail.com'
@contextmanager
def smtp_connect(username):
password = getpass.getpass('Enter email password for %s: ' % username)
logging.info('Connecting to %r', SMTPSERVER)
server = smtplib.SMTP(SMTPSERVER)
server.starttls()
server.login(username,password)
logging.info('Connected')
yield server
server.quit()
def sendmail(server, fromaddress, toaddress, subject, text, files):
msg = MIMEMultipart()
msg['From'] = fromaddress
msg['To'] = toaddress
msg['Cc'] = fromaddress
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
for f in files:
part = MIMEBase('application', "octet-stream")
with open(f, 'rb') as f_stream:
part.set_payload(f_stream.read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
msg.attach(part)
server.sendmail(fromaddress, toaddress, msg.as_string())
def main(argv):
logging.basicConfig(level=logging.INFO)
if len(argv) == 1 or argv[1] in ['-h', '--help']:
print 'Usage: %s <gmail username> <source files> ...' % argv[0]
sys.exit(1)
username = argv[1]
files = argv[2:]
useraddress = username + EMAILDOMAIN
with smtp_connect(username) as server:
for f in files:
shortname, _, _ = os.path.basename(f).partition('.')
logging.info('Sending solution for %s', shortname)
sendmail(server, useraddress, TOADDRESS, shortname, '', [f])
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment