在Go中分析电子邮件标题



如何在Go中读取电子邮件的标题?

通常我会使用ReadMIMEHeader(),但遗憾的是,并不是每个人都阅读了所有相关的RFC,对于一些消息,我会得到如下输出:

格式错误的MIME头行: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"

在消息源中。

去游乐场示例

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

如果消息格式不正确,我会通过一段单独的代码来修复它,重新格式化消息:

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()
}

游乐场:http://play.golang.org/p/OZsXT7pmtN

显然,您可能想要不同的启发式方法。我假设一行没有缩进并且不包含":",必须缩进。

退房https://github.com/sendgrid/go-gmime(免责声明,我使用SendGrid,但没有在库中组合任何内容)

最新更新