Reputation: 10939
How I can read some headers from an email message in Go?
Usually I would use ReadMIMEHeader(), but sadly not everybody has read all the relevant RFCs and for some messages I get output like:
malformed MIME header line: name="7DDA4_foo_9E5D72.zip"
I narrowed the culprit to be
Content-Type: application/x-zip-compressed; x-unix-mode=0600;
name="7DDA4_foo_9E5D72.zip"
instead of
Content-Type: application/x-zip-compressed; x-unix-mode=0600;
name="7DDA4_foo_9E5D72.zip"
in the source of the message.
What is the correct way of parsing the headers correctly, regardless if indented or not?
Upvotes: 0
Views: 2673
Reputation: 1397
Given that the message is malformed, I would fix it through a separate piece of code that reformats the message:
func fixBrokenMime(r_ io.Reader, w io.WriteCloser) {
r := bufio.NewScanner(bufio.NewReader(r_))
for r.Scan() {
line := r.Text()
if len(line) > 0 && line[0] != ' ' && strings.IndexByte(line, ':') < 0 {
line = " " + line
}
w.Write([]byte(line+"\n"))
}
w.Close()
}
Playground: http://play.golang.org/p/OZsXT7pmtN
Obviously, you may want a different heuristic. I assumed that a line that is not indented and doesn't contain ":", must be indented.
Upvotes: 1
Reputation: 771
Check out https://github.com/sendgrid/go-gmime (disclaimer, I work with SendGrid, but did not put together anything in the lib)
Upvotes: 0