[ACCEPTED]-django to send AND receive email?-django-email
There's an app called jutda-helpdesk that uses Python's 8
imaplib to process incoming emails. You just 7 have to have an account somewhere with POP3 6 or IMAP access.
This is adapted from their 5 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() for msg in messagesInfo: msgNum = msg.split(" ") msgSize = msg.split(" ") full_message = "\n".join(server.retr(msgNum)) # 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.split(): status, data = server.fetch(num, '(RFC822)') full_message = data # 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 4 server info, the rest should be pretty clear.
You'll 3 probably need to check the docs on
poplib and 2
imaplib to get specific parts of the message, but 1 hopefully this is enough to get you going.
I know this question is pretty old now but 5 just thought I'd add for future reference 4 that you might want to give http://cloudmailin.com a go. We have 3 quite a few django users using the system 2 and it should be a little simpler than the 1 proposed solution.
Django is really intended as a web server 28 (well, as a framework that fits into a web 27 server), not as an email server. I suppose 26 you could put some code into a Django web 25 application that starts up an email server, using 24 the kind of code shown in that question 23 you linked to, but I really wouldn't recommend 22 it; it's an abuse of the capabilities of 21 dynamic web programming.
The normal practice 20 is to have separate email and web servers, and 19 for that you would want to look into something 18 like Sendmail or (better yet) Postfix. For 17 POP3 you would also need something like 16 Dovecot or Courier, I think. (It's certainly 15 possible to have the email server notify 14 your web application when emails are received 13 so it can act on them, if that's what you 12 want to do.)
EDIT: in response to your comments: yes 11 you are trying to make (or at least use) an 10 email server. An email server is just a 9 program that receives emails (and may be 8 capable of sending them as well, but you 7 don't need that).
You could definitely write 6 a small email server in Python that just 5 receives these emails and saves the images 4 to the filesystem or a database or whatever. (Might 3 be worth asking a new question, about) But 2 don't make it part of your Django web app; keep 1 it as its own separate program.
More Related questions