What is the first step to getting in e-mail into my python / flask app code?

不打扰是莪最后的温柔 提交于 2019-12-01 05:03:15

问题


I am researching what it would take to make a web app that would interact with e-mails directly. Like you would send to something@myapp.com and the app would tear it apart and determine who it's from, if they are in the DB, what is the subject line, etc.

I am working with/most familiar with python and flask.

Could anyone get me started in the right direction of how to get an e-mail to interface with my flask app code?


回答1:


There are several approach you can take:

  • write some code which uses IMAP or POP to retrieve emails and process them. Either run this from a crontab (or something similar) or add it to your flask app and trigger it in there, either through a crontab that requests a magic URL or setting up a custom timer thread.
  • configure your MTA to deliver email for something@myapp.com by feeding it to a program you write (for example in Exim you could use a pipe transport) . In that program you can either process it directly, or do something like POSTing it to your flask app.



回答2:


I've done something along these lines recently, with a simple bookmarking web-app. I have the usual bookmarklet way of bookmarking something to it, but I also wanted to be able to e-mail links to it from apps like Reeder on my iPhone and whatever. You can see what I ended up with on GitHub: subMarks.

I use Google Apps for your Domain for my email, so I created a special address for my app to look at - I really didn't want to try building/configuring my own e-mail server.

the mail_daemon.py file from above is run as a cron job every 5 minutes. It connects to the email server using the poplib Python package, processes the emails that are there and then disconnects (one part I feel compelled to point out is that I check that the emails are from me before they are processed :) )

My Flask app then provides the front end to the bookmarks, displaying them from the database.

I decided not to put the email handling code into the actual flask app, because it can be rather slow and would only run when the page was visited, but you could do this if you wanted.

Here's some barebones code to get things going:

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's 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()


来源:https://stackoverflow.com/questions/13290962/what-is-the-first-step-to-getting-in-e-mail-into-my-python-flask-app-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!