如何在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
显然,您可能希望使用其他启发式方法。我认为必须缩进的行不缩进并且不包含“:”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句