2009-04-08 188 views
21

我對django的電子郵件發送功能非常熟悉,但是我沒有看到任何有關它接收和處理來自用戶的電子郵件的信息。此功能是否可用?django發送和接收電子郵件?

一些谷歌搜索沒有出現非常有希望的結果。雖然我確實發現:Receive and send emails in python

我會不得不推出自己的?如果是這樣,我會張貼該應用程序比你可以說...無論你說什麼。

感謝, 吉姆

更新:我沒有試圖讓電子郵件服務器,我只需要添加一些功能,你可以將照片用電子郵件發送到該網站,有它在彈出你帳戶。

+0

另請參閱此問題:http://stackoverflow.com/questions/640970/email-integration – 2009-04-09 10:32:41

回答

17

有一個名爲jutda-helpdesk的應用程序,它使用Python的poplibimaplib來處理傳入的電子郵件。您只需擁有POP3或IMAP訪問權限的帳戶。

這是改編自他們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只是一些對象來存儲所有的郵件服務器信息,其餘的應該很清楚。

您可能需要檢查poplibimaplib上的文檔以獲取消息的特定部分,但希望這足以讓您順利。

2

Django實際上是作爲一個web服務器(當然,作爲一個適合web服務器的框架),而不是一個電子郵件服務器。我想你可以將一些代碼放入一個啓動電子郵件服務器的Django Web應用程序中,使用你鏈接到的那個問題中顯示的代碼類型,但我真的不會推薦它;這是對動態網頁編程功能的濫用。

通常的做法是有單獨的電子郵件和網絡服務器,並且爲此您需要查看Sendmail或(更好)Postfix之類的東西。對於POP3,我想你也需要Dovecot或Courier之類的東西。 (這當然可能有收到郵件時,郵件服務器通知你的web應用程序,這樣就可以對其採取行動,如果這是你想要做什麼。)

編輯:響應您的意見:是的,你是試圖製作(或至少使用)電子郵件服務器。電子郵件服務器只是一個接收電子郵件的程序(並且可能也可以發送它們,但您不需要)。

你肯定可以在Python中編寫一個小型電子郵件服務器,它只接收這些電子郵件並將圖像保存到文件系統或數據庫或其他任何地方。 (可能值得提一個新的問題,但請不要將它作爲Django網絡應用程序的一部分;把它作爲自己的獨立程序。

+1

我不想製作一個電子郵件服務器,我只需要添加一些功能,您可以通過電子郵件將圖像發送到網站並將其彈出到您的帳戶中。 – Jiaaro 2009-04-09 12:16:17

5

我知道這個問題現在很老了,但只是想我會添加爲未來的參考,你可能想給http://cloudmailin.com一個去。我們有相當多的django用戶使用這個系統,它應該比提出的解決方案簡單一些。

+0

謝謝你史蒂夫,這正是我所期待的。 – 2013-02-23 13:04:23