开发者

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

I'm looking into a possible feature for my little to-do application... I like the idea that I can send an email to a particular email address, containing a to-do task I need to complete, and this will be read by my web application and be put in the database... So, when I come to log into my application, the to-do task I emailed wil开发者_运维知识库l be there as a entry in the app.

Is this possible? I have a slice with SliceHost (basically a dedicated server) so I have total control on what to install etc. I'm using Python/Django/MySQL for this.

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


If I were to implement this, I'd use a scheduler and a job to be scheduled.

That job would connect to the mail server (be it POP3 or IMAP) and parse the unread messages (or messages unread by the job). Based on that I would insert that record.

You'd get 2 types of records that way. A list of mail message ids which have been processed (so you don't reprocess mails) and a list of tasks.

Disadvantage is that it takes some time before you see the task, as the job only executes every X minutes, or seconds.

If that is not good enough I'd go for a permanent IMAP connection, but you'd have to implement more error handling; you don't just retry automatically every X minutes.

Googling for Django +scheduler will get you started.

also have a look at this StackOverflow thread, no need to reinvent the wheel :)


I needed the exact same thing. I use the Lamson project (which is written in python) to transform email, forward email based on rules to my www.evernote.com and thinking rock www.trgtd.com.au accounts, update firewall web filtering rules, update allow/deny lists for my spam filter, read and write databases etc....

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

www.lamsonproject.org

Troy


One way that I've solved this in the past was using qmail's .qmail files (docs).

Basically you set up qmail and point your email address (for ease of use, lets assume proc@whatever.com is your email address) to your home directory. In that directory you set up a .qmail-proc file to handle the mail.

This allows you to use a full-fledged SMTP server on your server, including spam filtering, forwarding, aliases, all that fun stuff. You can then pipe the data from an email into an application. In your case, I would suggest making a Mangement Command in Django to process the email (I'll call it proc_email). Thus your .qmail-proc may look like:

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

This stores a copy of the email in /var/spool/mail/proc, then passes the email to the script in the second line. The email itself is passed to proc_email via sys.stdin. Simply read the email from there, and store it through your Django Models.

If you need to process email for different addresses later, you can also set up aliases which point to your home directory, and use .qmail-<username> files for each alias. Allowing you to pass other flags (such as the username for each alias) to proc_email if needed.

I should note that this isn't the simplest solution, but it can scale, and is pretty darn bullet proof.


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.

I would then use just the Django ORM to update the database based on the emails received.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜