在Go语言中解析电子邮件消息头

huangapple go评论88阅读模式
英文:

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"

在消息源中。

Go Playground示例

无论是否缩进,解析标题的正确方法是什么?

英文:

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.

Go Playground example

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) &gt; 0 &amp;&amp; line[0] != &#39; &#39; &amp;&amp; strings.IndexByte(line, &#39;:&#39;) &lt; 0 {
			line = &quot; &quot; + line
		}
		w.Write([]byte(line+&quot;\n&quot;))
	}
	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)

huangapple
  • 本文由 发表于 2016年3月30日 05:23:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/36295343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定