【问题标题】:Downloading email attachments using python django使用 python django 下载电子邮件附件
【发布时间】:2016-03-01 06:10:45
【问题描述】:

我正在尝试用python开发一个邮件客户端

我能够解析带有附件的电子邮件正文并显示在我的 django 模板中。

现在我需要在点击附件名称时下载附件。

我能找到的只是使用 python 将文件下载到特定文件夹的方法。 但是当我点击浏览器上的文件名时,如何将它下载到系统的默认下载文件夹中

下面是我试过的代码示例

def download_attachment(request):
    if request.method == 'POST':
        filename=request.POST.get('filename','')
        mid=request.POST.get('mid','')
        mailserver = IMAP_connect("mail.example.com",username, password)
        if mailserver:
            mailserver.select('INBOX')
        result, data = mailserver.uid('fetch', mid, "(RFC822)")
        if result == 'OK':
            mail = email.message_from_string(data[0][1])
            for part in mail.walk():
                if part.get_content_maintype() == 'multipart':
                    continue
                if part.get('Content-Disposition') is None:
                    continue
                fileName = part.get_filename()
                if filename != fileName:
                    continue
                detach_dir = '.'
                if 'attachments' not in os.listdir(detach_dir):
                    os.mkdir('attachments')
                if bool(fileName):
                    filePath = os.path.join(detach_dir, 'attachments', fileName)
                    if not os.path.isfile(filePath) :
                        print fileName
                        fp = open(filePath, 'wb')
                        fp.write(part.get_payload(decode=True))
                        fp.close()
    return HttpResponse() 

【问题讨论】:

    标签: python django email-attachments


    【解决方案1】:

    您无法从 django 访问系统默认下载文件夹的名称。这取决于用户在他/她的浏览器设置中决定。你可以做的是告诉浏览器通过设置Content-Disposition将文件视为附件,然后它将打开正常的“另存为...”框,默认为下载文件夹。

    实现这一点的一些 django 代码如下所示:

    response = HttpResponse()
    response['Content-Disposition'] = 'attachment; filename="%s"' % fileName
    return response
    

    另见this question

    【讨论】:

    • 你的意思是用这个来改变我的最后一个 if 循环内容?那我们怎么会有只有 content-disposition 的文件内容?我不想在我的服务器中创建文件副本并读取它并作为响应发送
    • 是的,您只需在响应对象中包含您想要发送的数据。您可以使用response.write(data) 或在创建响应时仅传递数据,例如response = HttpResponse(data)
    【解决方案2】:

    下面的代码运行得很好

    def download_attachment(request):
        if request.method == 'GET':
            filename=request.GET.get('filename','')
            mid=request.GET.get('mid','')
            mailserver = IMAP_connect("mail.example.com",username, password)
            if mailserver:
                mailserver.select('INBOX')
            result, data = mailserver.uid('fetch', mid, "(RFC822)")
            if result == 'OK':
                mail = email.message_from_string(data[0][1])
                for part in mail.walk():
                    if part.get_content_maintype() == 'multipart':
                        continue
                    if part.get('Content-Disposition') is None:
                        continue
                    fileName = part.get_filename()
                    if filename != fileName:
                        continue
                    if bool(fileName):
                        response = HttpResponse(part.get_payload(decode=True))
                        response['Content-Disposition'] = 'attachment; filename="%s"' % fileName
                        return response 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 2017-10-07
      相关资源
      最近更新 更多