Created
October 30, 2012 17:59
-
-
Save jdlrobson/3981892 to your computer and use it in GitHub Desktop.
Email digest for Wikipedia Feedback
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
from config import config | |
import poplib | |
import smtplib | |
import time | |
import email as emailpy | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEText import MIMEText | |
import simplejson, httplib2 | |
DIVIDER = '\n*****************************\n*****************************\n*****************************\n' | |
def pop_send_mail(s, email): | |
sender = config['sender'] | |
msg = MIMEMultipart( 'alternative' ) | |
msg['Subject'] = email['subject'] | |
msg['From'] = email["from"] | |
addressList = [] | |
if "bcc" in email: | |
if type( email["bcc"] ) == type( "" ): | |
addressList.append(email["bcc"]) | |
elif type(email["bcc"]) == type([]): | |
addressList.extend(email["bcc"]) | |
print 'email construction' | |
part1 = MIMEText(email["body"], 'plain') | |
msg.attach(part1) | |
mailBody = msg.as_string() | |
print 'sending to %s'%addressList | |
s.sendmail(msg["From"],addressList,mailBody) | |
print "message sent" | |
pass | |
def mail_agent( args, args2, args3 ): | |
print "booting up mail agent" | |
try: | |
settings = config['mailer'] | |
except KeyError: | |
print "mail agent failed to start: add to your config 'mailer':{'pop':'','username':'','password':''}" | |
return | |
server = settings["pop"] | |
user = settings["username"] | |
password = settings["password"] | |
MEmail = { "empty": True, "from": sender, "bcc": args, "body": "Feedback for Wikipedia Mobile Site:\n\n", "subject": "Wikipedia Mobile Feedback" } | |
WMmail = { "empty": True, "from": sender, "bcc": args2, "body": "Feedback for Wikipedia Mobile App:\n\n", "subject": "Wikipedia App Feedback" } | |
SPAMmail = { "empty": True, "from": sender, "bcc": args3, "body": "Feedback for Wikipedia that is probably spam or doesn't contain much information:\n\n", "subject": "Wikipedia Feedback (Spam)" } | |
def isSpam( body ): | |
matches = [] | |
try: | |
x = body.index( 'MobileFrontend' ) | |
matches.append( 'MFE' ) | |
print 'This is MobileFrontend feedback' | |
except ValueError: | |
pass | |
try: | |
x = body.index( 'WikipediaMobile' ) | |
matches.append( 'WikiMobile' ) | |
except ValueError: | |
pass | |
if len( body ) < 280: | |
print "mail too short (%s)"%len( body ) | |
return True | |
if len( matches ) == 0: | |
print "Not sure if this is Wikipedia Mobile or MFE feedback." | |
return True | |
else: | |
print "Not spam." | |
return False | |
def isAppFeedback( body ): | |
try: | |
body.index( 'WikipediaMobile/' ) | |
return True | |
except ValueError: | |
return False | |
print "logging %s into %s"%( user,server ) | |
try: | |
session = poplib.POP3( server ) | |
session.user( user ) | |
session.pass_( password ) | |
except Exception,e: | |
print "failed to start mail agent. Will try again in 30s." | |
time.sleep( 5 ) | |
return mail_agent( args ) | |
print "logged into mail agent" | |
numMessages = len( session.list()[1] ) | |
print "there are %s new messages"%numMessages | |
for i in range( 1, numMessages+1 ): | |
print "ooohh a message!! :D :D :D" | |
raw_email = session.retr( i )[1] | |
email_string = "" | |
for el in raw_email: | |
email_string += el | |
email_string += "\n" | |
msg = emailpy.message_from_string( email_string ) | |
body = msg.get_payload() | |
if type( body ) == type( [] ): | |
body = body[0].as_string() | |
try: | |
body.index("\n\n" ) | |
body = body[body.index( "\n\n" ):] | |
except ValueError: | |
body = body | |
sender = msg['From'] | |
spamscore = msg['X-Spam-Score'] | |
if isSpam( body ) or float( spamscore ) < -2: | |
print 'add spam mail to digest' | |
SPAMmail["empty"] = False | |
SPAMmail["body"] = SPAMmail["body"] + DIVIDER + '%s: %s (%s) Length: %s\n'%( i, sender, spamscore, len( body ) ) + body | |
else: | |
if isAppFeedback( body ): | |
print 'add app feedback mail to digest' | |
WMmail["empty"] = False | |
WMmail["body"] = WMmail["body"] + DIVIDER + sender + '\n' + body | |
else: | |
print 'add mobile site mail to digest' | |
MEmail["empty"] = False | |
MEmail["body"] = MEmail["body"] + DIVIDER + sender + '\n' + body | |
session.dele( i ) #throw away the parsed e-mail | |
#send digest emails | |
print "logging in" | |
s=smtplib.SMTP() | |
try: | |
smtp = config['mailer']['smtp'] | |
user = config['mailer']['username'] | |
password = config['mailer']['password'] | |
except KeyError: | |
print "please add {mailer:'smtp':'','user':'','password':''} to your config file" | |
return | |
s.connect(smtp) | |
s.login(user,password) | |
print 'logged in to smtp and able to send' | |
if not WMmail["empty"]: | |
try: | |
print "sending app digest" | |
pop_send_mail( s, WMmail ) | |
except Exception,e: | |
print "error sending App mail %s (%s)"%( WMmail, e) | |
if not MEmail["empty"]: | |
try: | |
print "sending mobile digest" | |
pop_send_mail( s, MEmail ) | |
except Exception,e: | |
print "error sending mobile site email %s (%s)"%( MEmail, e ) | |
if not SPAMmail["empty"]: | |
try: | |
print "sending spam digest" | |
pop_send_mail( s, SPAMmail ) | |
except Exception,e: | |
print "error sending mobile site email %s (%s)"%(SPAMmail, e ) | |
s.quit() | |
session.quit() | |
mail_agent( | |
config['receiver']['site'], | |
config['receiver']['app'], | |
config['receiver']['spam'] ) |
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
config = { | |
'sender': 'a@b.com', | |
'mailer':{'smtp':'xxx','pop':'xxxx','username':'xxxx','password':'xxxx' }, | |
'receiver': { | |
'site': [ 'x@y.com' ], | |
'app': [ 'x@y.com' ], | |
'spam': [ 'x@y.com' ] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment