Django收到电子邮件及其附件

问题描述:

在我工作的应用程序中,我想从我的Gmail帐户中获取我的电子邮件。所以我设置了一个存储电子邮件地址,端口,服务器和密码的类。Django收到电子邮件及其附件

我创建了一个函数,可以从gmail帐户电子邮件中获得Subject,Body和From部分,并将备份文件夹中的邮件备份为“.eml”扩展名。此外,我有一个“mail-list.html”模板,其中列表正在显示上面的标题和内容。一切都好,直到这里。

现在我怎么能得到attchment,如果有任何消息,所以我可以在我的“mail-list.html”模板中显示,如果有的话。 电子邮件的东西对我来说是全新的,所以任何示例代码甚至指向我的方向都会很棒!

我检查了一些插件,如Django邮箱,但我希望它是我最后的手段。

在我工作的应用程序中,我想从我的Gmail帐户中获取我的电子邮件。所以我设置了一个存储电子邮件地址,端口,服务器和密码的类。

我创建了一个函数,可以从gmail帐户电子邮件中获得Subject,Body和From部分,并将备份文件夹中的邮件备份为“.eml”扩展名。此外,我有一个“mail-list.html”模板,其中列表正在显示上面的标题和内容。一切都好,直到这里。

现在我怎么能得到attchment,如果有任何消息,所以我可以在我的“mail-list.html”模板中显示,如果有的话。 电子邮件的东西对我来说是全新的,所以任何示例代码甚至指向我的方向都会很棒!

我检查了一些插件,如Django邮箱,但我希望它是我最后的手段。

UPDATE:

我必须设法得到这样的附件...

#previous code here to get subject,body etc in my function 

if message.get_content_maintype() == 'multipart': 
    filenames_list = []   
    for part in message.walk(): 
     print("part.get_content_maintype ",part.get_content_maintype()) 
     #find the attachment part - so skip all the other parts 

     if part.get_content_maintype() == 'multipart': continue 
     if part.get_content_maintype() == 'text': continue 
     if part.get('Content-Disposition') == 'inline': continue 
     if part.get('Content-Disposition') is None: continue 

     #put attachments in list 
     filenames_list.append(filename) 
     print ("filenames_list",filenames_list) 

     #create context for template 
     mail_list['attachment'] = filenames_list 

所以,现在,我把我的文件名列表,并在我的模板我使用它们把它们放在mail_list ['attachment']上下文中。

当文件名是在英语中我得到这样的: [“myessay.pdf”,“test.odt”]

但是,当附件是在不同的语言(例如希腊)获得:['=?UTF-8?B?zrXOs86zz4HOsc + Gzr8xLmRvYw ==?=','=?UTF-8?B?zrXOs86zz4HOsc + Gzr8yLmRvYw ==?=','=?UTF-8?B?zrXOs86zz4HOsc + Gzr8xMi5kb2M = ?=']

正如你在上面看到的,在列表中有三个附件,由“,”分隔。

如何解码或编码它们?我不知道这里适合什么。

+0

哪些邮件服务您使用? –

+0

我从浏览器使用Gmail。它的工作与任何服务虽然..我也试过office365。 –

所以我想通了......我希望这可以帮助别人.. 我指的是获取附件的代码部分。

#some code before to require the server variable from the model where 
#I store all the required fields for the connction(port,e-mail address etc) 
... 
... 
... 


pop3 = poplib.POP3_SSL(server) # or "POP3" if SSL is not supported 
msg_list = pop3.list() 
pop3.list() 
#Get messages from server: 
messages = [pop3.retr(i) for i in range(1, len(pop3.list()[1]) + 1)] 
# Concat message pieces: 
messages = ["\n".join(m[1]) for m in messages] 
#Parse message intom an email object: 
messages = [parser.Parser().parsestr(m) for m in messages] 
for message in messages: 
    mail_list = {} 
    for item in message.items(): 
     #code to get subject,from,date 
     ... 
     ... 

     #and now to get the attachments of each message 

     for part in message.walk(): 
      #find the attachment part - so skip all the other parts 
      if part.get_content_maintype() == 'multipart': continue 
      if part.get_content_maintype() == 'text': continue 
      if part.get('Content-Disposition') == 'inline': continue 
      if part.get('Content-Disposition') is None: continue 

       #get filename 
       filename = part.get_filename() 
       #this is for encoding other than latin-letter-languages 
       if decode_header(filename)[0][1] is not None: 
        filename = str(decode_header(filename[0][0]).decode(decode_header(filename)[0][1]) 
      filenames_list.append(filename) 
      mail_list['attachment'] = filenames_list 

context['messages'] = mail_list 

在我的模板,为了使文件正常显示我也有过这样的“附件”迭代:

{% for d in messages %} 

/*subject,from,date = parts of the message I have got inside the function before the code I am displaying*/ 

{{ d.subject }} 
{{ d.from }} 
{{ d.date }} 

{% for t in d.attachment %}{{ t }}{% endfor %} 

{% endfor %}