What is the first step to getting email in my python / flask application code?

I'm learning what it takes to make a web application that will interact directly with email. For example, you would send to the address something@myapp.com , and the application will tear it into pieces and determine who it is connected from, if they are in the database, what is the subject line, etc.

I work with / are most familiar with python and jar.

Can someone lead me in the right direction, how to get an email for the interface with my flash code?

+4
source share
2 answers

There are several ways:

  • write code that uses IMAP or POP to receive emails and process them. Either run it from crontab (or something similar), or add it to your flash application and enable it there, either through crontab, which requests a magic URL, or sets up a custom timer stream.
  • configure your MTA to deliver email to something@myapp.com by sending it to the program you are writing (for example, in Exim you can use pipe transport ). In this program, you can either process it directly, or do something like POSTing in your flash application.
+2
source

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() 
0
source

Source: https://habr.com/ru/post/1444813/


All Articles