1

I am trying to develop a mail client in python

I am able to parse the email body with attachment and display in my django template.

Now I need to download the attachment when I click on the attachment name.

All I could find is the way to download file to a specific folder using python. But How could I download it to the default downloads folder of system when I click on the filename on my browser

Below is the code sample I tried

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() 
ilse2005
  • 11,189
  • 5
  • 51
  • 75
JithPS
  • 1,167
  • 1
  • 7
  • 19

2 Answers2

1

You can't access the name of the system's default downloads folder from django. That's up to the user to decide in his/her browser settings. What you can do is to tell the browser to treat the file as an attachment by setting Content-Disposition, and then it will open the normal "Save as..." box which will default to the downloads folder.

Some django code that makes this happen would look like:

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

See also this question.

Community
  • 1
  • 1
Dan Russell
  • 960
  • 8
  • 22
  • You mean to change my last if loop content with this??Then how come we will have the file content with only content-disposition?? I dont want to create a copy of file in my server and read it and send as a response – JithPS Mar 02 '16 at 07:05
  • Yeah, you just need to include the data you want sent in your response object. You can use `response.write(data)` or just pass the data when you create the response, so like `response = HttpResponse(data)`. – Dan Russell Mar 02 '16 at 15:23
0

The below code worked really well

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 
JithPS
  • 1,167
  • 1
  • 7
  • 19