In Django, I want to insert a database record by sending me an email?

I am looking at a possible function for my small application ... I like the idea that I can send an email to a specific email address containing the task that I need to complete, and this will be read by my web application and put into the database. .. So, when I come to the entrance to my application, the task that I sent by email will be there as a record in the application.

Is it possible? I have a fragment with SliceHost (basically a dedicated server), so I have full control over what to install, etc. I am using Python / Django / MySQL for this.

Any ideas on what steps to take to make this happen?

+4
source share
4 answers

If I implemented this, I would use the scheduler and the task that should be scheduled.

This work will connect to the mail server (whether it be POP3 or IMAP) and analyze unread messages (or messages unread by the job). Based on this, I would insert this entry.

This way you get 2 types of records. The list of identifiers of mail messages that were processed (therefore, you do not process letters) and the list of tasks.

The disadvantage is that it takes some time before you see the task, because the task runs only every X minutes or seconds.

If this is not good enough, I would go for a permanent IMAP connection, but you would have to do more error handling; you do not just repeat automatically every X minutes.

Googling for the Django + Scheduler helps you get started.

also see fooobar.com/questions/17924 / ... , no need to reinvent the wheel :)

+5
source

I needed the same thing. I use the Lamson project (which is written in python) to convert emails, forward rule-based emails to my www.evernote.com and think rock www.trgtd.com.au accounts, update web filtering rules, update enable / disable lists for my spam filter, database for reading and writing, etc.

I like to think of it as email automation and email application development.

www.lamsonproject.org

Troy

+2
source

One of the ways I've decided in the past is to use qmail .qmail files ( docs ).

Basically, you set up qmail and enter your email address (for convenience, assume that proc@whatever.com is your email address) in your home directory. In this directory, you create a .qmail-proc file to process mail.

This allows you to use a full-fledged SMTP server on your server, including spam filtering, forwarding, aliases, all these funny things. You can then transfer the data from the email to the application. In your case, I would suggest making a Mangement Command in Django to handle email (I will call it proc_email ). So your .qmail-proc might look like this:

 /var/spool/mail/proc | /www/django/myproject/manage.py proc_email 

This is where a copy of the email message is stored in /var/spool/mail/proc , and then an email is sent to the script in the second line. The message itself is sent to proc_email via sys.stdin . Just read the letter from there and save it through your Django models.

If you need to process emails for different addresses later, you can also set up aliases pointing to your home directory and use .qmail-<username> files for each alias. Allow you to pass other flags (e.g. username for each alias) to proc_email if necessary.

I should point out that this is not the easiest solution, but it can scale and is pretty damn proof of a bullet.

+1
source

I would not focus on Django for this.

I would create a mail server to catch these emails. Use http://docs.python.org/library/smtpd.html .

Then I would use only Django ORM to update the database based on the emails received.

0
source

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


All Articles