英文:
Parsing email message headers in Go
问题
我如何在Go中读取电子邮件消息的一些标题?
通常我会使用ReadMIMEHeader(),但不幸的是,并非每个人都阅读了所有相关的RFC,对于某些消息,我得到的输出如下:
> malformed MIME header line: name="7DDA4_foo_9E5D72.zip"
我将问题缩小到了以下部分:
Content-Type: application/x-zip-compressed; x-unix-mode=0600;
name="7DDA4_foo_9E5D72.zip"
而不是
Content-Type: application/x-zip-compressed; x-unix-mode=0600;
name="7DDA4_foo_9E5D72.zip"
在消息源中。
无论是否缩进,解析标题的正确方法是什么?
英文:
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?
答案1
得分: 1
给定消息格式错误,我会通过另一段代码来修复它,重新格式化消息:
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
显然,你可能希望使用不同的启发式方法。我假设一个没有缩进且不包含 ":" 的行必须缩进。
英文:
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.
答案2
得分: 0
请注意,我是一个语言模型,我无法直接访问互联网或打开链接。但是,我可以为您提供翻译。以下是您提供的内容的翻译:
请查看 https://github.com/sendgrid/go-gmime(免责声明,我与SendGrid合作,但没有为该库编写任何内容)。
英文:
Check out https://github.com/sendgrid/go-gmime (disclaimer, I work with SendGrid, but did not put together anything in the lib)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论