tags:

views:

1577

answers:

3

Hello, I have gotten quite familiar with django's email sending abilities, but I havn't seen anything about it receiving and processing emails from users. Is this functionality available?

A few google searches have not turned up very promising results. Though I did find this: http://stackoverflow.com/questions/348392/receive-and-send-emails-in-python

Am I going to have to roll my own? if so, I'll be posting that app faster than you can say... whatever you say.

thanks, Jim

update: I'm not trying to make an email server, I just need to add some functionality where you can email an image to the site and have it pop up in your account.

+1  A: 

Django is really intended as a web server (well, as a framework that fits into a web server), not as an email server. I suppose you could put some code into a Django web application that starts up an email server, using the kind of code shown in that question you linked to, but I really wouldn't recommend it; it's an abuse of the capabilities of dynamic web programming.

The normal practice is to have separate email and web servers, and for that you would want to look into something like Sendmail or (better yet) Postfix. For POP3 you would also need something like Dovecot or Courier, I think. (It's certainly possible to have the email server notify your web application when emails are received so it can act on them, if that's what you want to do.)

EDIT: in response to your comments: yes you are trying to make (or at least use) an email server. An email server is just a program that receives emails (and may be capable of sending them as well, but you don't need that).

You could definitely write a small email server in Python that just receives these emails and saves the images to the filesystem or a database or whatever. (Might be worth asking a new question, about) But don't make it part of your Django web app; keep it as its own separate program.

David Zaslavsky
I'm not trying to make an email server, I just need to add some functionality where you can email an image to the site and have it pop up in your account.
Jiaaro
+5  A: 

There's an app called jutda-helpdesk that uses Python's poplib and imaplib to process incoming emails. You just have to have an account somewhere with POP3 or IMAP access.

This is adapted from their get_email.py:

def process_mail(mb):
    print "Processing: %s" % q
    if mb.email_box_type == 'pop3':
        if mb.email_box_ssl:
            if not mb.email_box_port: mb.email_box_port = 995
            server = poplib.POP3_SSL(mb.email_box_host, int(mb.email_box_port))
        else:
            if not mb.email_box_port: mb.email_box_port = 110
            server = poplib.POP3(mb.email_box_host, int(mb.email_box_port))
        server.getwelcome()
        server.user(mb.email_box_user)
        server.pass_(mb.email_box_pass)

        messagesInfo = server.list()[1]

        for msg in messagesInfo:
            msgNum = msg.split(" ")[0]
            msgSize = msg.split(" ")[1]
            full_message = "\n".join(server.retr(msgNum)[1])

            # Do something with the message

            server.dele(msgNum)
        server.quit()

    elif mb.email_box_type == 'imap':
        if mb.email_box_ssl:
            if not mb.email_box_port: mb.email_box_port = 993
            server = imaplib.IMAP4_SSL(mb.email_box_host, int(mb.email_box_port))
        else:
            if not mb.email_box_port: mb.email_box_port = 143
            server = imaplib.IMAP4(mb.email_box_host, int(mb.email_box_port))
        server.login(mb.email_box_user, mb.email_box_pass)
        server.select(mb.email_box_imap_folder)
        status, data = server.search(None, 'ALL')
        for num in data[0].split():
            status, data = server.fetch(num, '(RFC822)')
            full_message = data[0][1]

            # Do something with the message

            server.store(num, '+FLAGS', '\\Deleted')
        server.expunge()
        server.close()
        server.logout()

mb is just some object to store all the mail server info, the rest should be pretty clear.

You'll probably need to check the docs on poplib and imaplib to get specific parts of the message, but hopefully this is enough to get you going.

tghw
+1  A: 

I think you might be looking for this: http://code.google.com/p/django-webmail/

very useful :) thanks for pointing me that way! I didn't end up using it as I've already implemented our mail recieving code, but from my initial glance, it seems I've done it the same way as the django--webmail app
Jiaaro