views:

492

answers:

5

I'd like to provide a functionality for users of my website to get assigned an email address upon registration (such as [email protected]) but I don't really think it is feasible to actually support all these emails account normally through a webmail program. I am also not sure if my webhost would be cool with it. What I'd really want is to be able to have a seamless integration of this email into the bigger system that the website is, as it is mostly going to be for intra-site messaging but we want to allow users to put actual email addresses. So what I would like to do instead is have a catch-all account under mydomain and have this email look at incoming mail, see who it was meant to be sent to, and add a message for the user in the system.

So, the questions are:

1) Is this the right approach? How expensive would it be to get a host that would allow me to just assign emails to at will to my domain? I am currently using WebFaction's shared hosting.
2) If it is an okay approach, what is the best way to route this catch all account to my python script? I have read about .forward but I am not very good at UNIX stuff. Once I figure that out, how would I get the script to be in the "Django environment" so I can use Django's model functionality to add the new messages to the user?
3) Is there anything Django can do to make this any easier?
4) Are there any tools in Python to help me parse the email address? How do they work?

A: 

but I don't really think it is feasible to actually support all these emails account normally through a webmail program

I think that your base assumption here is incorrect. You see, most 'webmail' programs are just frontends (or clients) to the backend mail system (postfix etc). You will need to see how your webhost is set up. There is no reason why you can not create these accounts programmatically and then let them use a normal webmail interface like SquirrelMail or RoundCube. For instance, my webhost (bluehost) allows me 2500 email accounts - I am not sure how many yours allows - but I can upgrade to unlimited for a few extra dollars a month. I think that using the builtin email handling facility is a more robust way to go.

Shane C. Mason
A: 

What I meant by not really feasible is that its not for my purposes as I want/need to have the emails be integrated to the website as their primary function is going to be as intra-site messaging system between users. The email functionality is an after-thought/addon. I want the users to remain "in the site" while they're checking their messages, not sent off to a foreign web mail client. So with that in mind I don't really think its worth it to technically have an email account for every single one of them, I just figured I might as well have a catch-all that can determine what to do.

(Sorry about adding an answer but the browser cookie that recognized me is gone for some reason...)

Then in my opinion, email is not the correct solution. You should just build a simple Django model to handle intrasite messaging system.
Shane C. Mason
That is what I am doing... but I'd still like for them to be able to receive emails from the "outside world" into this intrasite messaging system. It doesn't have to be terribly robust, just something that can handle most incoming emails
A: 

Your question is similar to this question.

Use a project like django-messages to handle messaging between your users.

If you want to let users receive mail from outside your Django site then you will need to set up an MTA to handle receiving and storing the email, then something like procmail to retrieve it into your Django message database.

Common MTA's are postfix, exim, and qmail. Python based ones listed in answers to this question

You'll also need to roll your own code to make each new user on your Django site a valid email recipient so they won't be rejected by the MTA.

Van Gale
+1  A: 

To directly answer your questions:

1,2) Check out this FAQ in the WebFaction website. It explains how to easily route incoming emails into the script of your choice. When creating an email address, you can just not specify a username to make it be a catch-all email that anything sent to the domain goes to.

3) As others have suggested, you could check out django-messages, but maybe Django Plugables has something better.

4) Check out the email.parser module as it takes care of most of the scary parts of parsing emails.

Paolo Bergantino
+1  A: 

See my answer to a similar question. It has all the basic code to get you started with an email parser for Django.

Edit: On second thought, here's the code:

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