mxk / go-imap

IMAP4rev1 Client for Go
BSD 3-Clause "New" or "Revised" License
212 stars 63 forks source link

BODYSTRUCTURE #17

Open pedromorgan opened 9 years ago

pedromorgan commented 9 years ago

How do I parse BODYSTRUCTURE, as I want to find the attachments..

Using

cmd, err := imap.Wait( client.UIDFetch(uidlist, "FLAGS", "INTERNALDATE", "RFC822.SIZE", "RFC822.HEADER", "BODYSTRUCTURE") )

and then,

for _, bsv := range imap.AsList(rsp.MessageInfo().Attrs["BODYSTRUCTURE"]) {
    if  imap.TypeOf(bsv) == imap.List {
        vvv := imap.AsList(bsv)
        fmt.Println(" ==", vvv)
    }
}

Prints

 == [["text" "plain" ["charset" "UTF-8"] <nil> <nil> "7bit" 34 1 <nil> <nil> <nil> <nil>] ["text" "html" ["charset" "UTF-8"] <nil> <nil> "7bit" 55 1 <nil> <nil> <nil> <nil>] "alternative" ["boundary" "001a1145bb103402220524805d18"] <nil> <nil> <nil>]

 == ["application" "pdf" ["name" "White Paper - On-train fiber optic connectivity.pdf"] <nil> <nil> "base64" 1718302 <nil> ["attachment" ["filename" "White Paper - On-train fiber optic connectivity.pdf"]] <nil> <nil>]

 == ["application" "pdf" ["name" "Interim Report 23.11.14.pdf"] <nil> <nil> "base64" 447908 <nil> ["attachment" ["filename" "Interim Report 23.11.14.pdf"]] <nil> <nil>]

 == ["image" "jpeg" ["name" "ana.jpeg"] <nil> <nil> "base64" 882306 <nil> ["attachment" ["filename" "ana.jpeg"]] <nil> <nil>]

 == ["boundary" "001a1145bb103402290524805d1a"]
mxk commented 9 years ago

Use the As* functions to navigate the returned structure. See https://github.com/mxk/go-imap/blob/master/imap/response.go#L266 for an example and https://tools.ietf.org/html/rfc3501#section-7.4.2 for a description of BODYSTRUCTURE. There is also a test example: https://github.com/mxk/go-imap/blob/master/imap/reader_test.go#L519.