views:

181

answers:

1

First of all i don't have the code example on this computer, but i have an example that is quite similar.

(http://docs.python.org/library/email-examples.html)

The 4th one.

My issue lies within this bit of code

counter = 1
for part in msg.walk():
    # multipart/* are just containers
    if part.get_content_maintype() == 'multipart':
        continue
    # Applications should really sanitize the given filename so that an
    # email message can't be used to overwrite important files
    filename = part.get_filename()
    if not filename:
        ext = mimetypes.guess_extension(part.get_content_type())
        if not ext:
            # Use a generic bag-of-bits extension
            ext = '.bin'
        filename = 'part-%03d%s' % (counter, ext)
    counter += 1
    fp = open(os.path.join(opts.directory, filename), 'wb')
    fp.write(part.get_payload(decode=True))
    fp.close()

When i fetch emails that do not have iso or utf encoded filenames, this code works fine. But when the attachment name is iso encoded, the filename is not within the get_filename, but the filename is in encoded form within part["Content-type"] (i belive)

The above example tries to guess the extension and if it cant find the filename, it just gives it a part filename. What i would like is the filename.

Has anyone dealt with issues like these, and what did you do to fix it?

A: 

I found the issue, it was with

mimetypes.guess_extension(part.get_content_type())

And images with "image/pjpeg" as the content type

@S.Lott i have changed the code to resemble the above example, but i added this to fix the pjpeg issue.

if not filename:
 ext = mimetypes.guess_extension(part.get_content_type())

if not ext:
    guess = part["Content-Type"].split(";")

    if guess[0] == "image/pjpeg":
        guess[0] = "image/jpeg"

    ext = mimetypes.guess_extension(guess[0])

if not ext:
    ext = ".bin"
Ólafur Waage