I recently did something like that with a simple bookmarking web application. I have the usual way of bookmarking something, but I also wanted to be able to email links to it from applications such as Reeder on my iPhone, etc. You can see that I am done with GitHub: subMarks .
I use Google Apps for my domain for my email, so I created a special address for my application to see - I really did not want to try to create / configure my own email server.
The mail_daemon.py file above runs as a cron job every 5 minutes. It connects to the email server using the Python poplib package, processes the emails that are there, and then disconnects (the one part that I feel is forced to indicate that I check that the emails are from me before they are processed :))
My Flask provides the front of bookmarks by displaying them from the database.
I decided not to enter the email processing code in the current flash application, because it can be quite slow and will only work when visiting a page, but you could do it if you want.
Here is some barebones code to make it work:
import poplib from email import parser from email.header import decode_header import os import sys pop_conn = poplib.POP3_SSL('pop.example.com') pop_conn.user(' my-app@example.com ') pop_conn.pass_('password') #Get messages from server: messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] # Concat message pieces: messages = ["\n".join(mssg[1]) for mssg in messages] #Parse message into an email object: messages = [parser.Parser().parsestr(mssg) for mssg in messages] for message in messages: # check message is from a safe recipient if ' me@example.com ' in message['from']: # Get the message body text if message['Content-Type'][:4] == 'text': text = message.get_payload() #plain text messages only have one payload else: text = message.get_payload()[0].get_payload() #HTML messages have more payloads # decode the subject (odd symbols cause it to be encoded sometimes) subject = decode_header(message['subject'])[0] if subject[1]: bookmark_title = subject[0].decode(subject[1]).encode('ascii', 'ignore') # icky else: bookmark_title = subject[0] # in my system, you can use google address+tag@gmail.com feature to specifiy # where something goes, a useful feature. project = message['to'].split('@')[0].split('+') ### Do something here with the message ### pop_conn.quit()
source share