Get attachment filename for quirky mailers

63 views
Skip to first unread message

Reinhard Wobst

unread,
Nov 13, 2023, 8:36:45 AM11/13/23
to golang-nuts
This is not a question, just a hint. Recently I wrote a program which
extracts attachments from multipart mime mails stored as files. I did
this since there are often mails with many attachments (e.g., pictures),
and before all, they have unwanted filenames - windows-encoded, strange
characters, with blanks, 'JPEG' suffix instead of jpg if your scripts
work with 'jpg' files only ...

Makeing nice filenames (including RFC 2047 decoding) was not the problem.
However, some quirky mailers (e.g. web.de web mailer) do not have a
'filename' parameter in the 'Content-Disposition' header but only a 'name'
parameter in the 'Content-Type' header.

So in this case it is recommended to write

    mediaType, params, err = mime.ParseMediaType(part.Header.Get("Content-Type"))
    ...
    fn := part.FileName()
                   
    if len(fn) == 0 {
            fn = params["name"]
    }

And then fn can be 'niced' as you want; if fn is empty, a filename
should be requested (otherwise, confirm the proposal).
Reply all
Reply to author
Forward
0 new messages