diff --git a/readme.md b/readme.md index 74aa75a..6266f0c 100644 --- a/readme.md +++ b/readme.md @@ -24,11 +24,9 @@ ### 安装使用 -1.git clone https://github.com/taoshihan1991/imaptool.git +1.在$GOPATH的src/github.com下, git clone https://github.com/taoshihan1991/imaptool.git -2.进入目录执行 go mod tidy +2.源码运行 go run server.go -3.源码运行 go run server.go - -4.源码打包 go build server.go +3.源码打包 go build server.go diff --git a/tools/imap.go b/tools/imap.go index 2dcf9cb..db19ce8 100644 --- a/tools/imap.go +++ b/tools/imap.go @@ -179,15 +179,15 @@ func GetMessage(server string, email string, password string, folder string, id if r == nil { log.Fatal("Server didn't returned message body") } + var mailitem = new(MailItem) // Create a new mail reader mr, err := mail.CreateReader(r) + if err != nil { - //log.Fatal(err) + return mailitem } - var mailitem = new(MailItem) - // Print some info about the message header := mr.Header date, _ := header.Date() @@ -226,9 +226,9 @@ func GetMessage(server string, email string, password string, folder string, id log.Println("Subject:", s) mailitem.Subject = s // Process each message's part - var bodyMap=make(map[string]string) - bodyMap["text/plain"]= "" - bodyMap["text/html"]= "" + var bodyMap = make(map[string]string) + bodyMap["text/plain"] = "" + bodyMap["text/html"] = "" for { p, err := mr.NextPart() @@ -242,11 +242,11 @@ func GetMessage(server string, email string, password string, folder string, id // This is the message's text (can be plain-text or HTML) b, _ := ioutil.ReadAll(p.Body) - ct:=p.Header.Get("Content-Type") - if strings.Contains(ct,"text/plain"){ - bodyMap["text/plain"]+=Encoding(string(b),ct) - }else{ - bodyMap["text/html"]+=Encoding(string(b),ct) + ct := p.Header.Get("Content-Type") + if strings.Contains(ct, "text/plain") { + bodyMap["text/plain"] += Encoding(string(b), ct) + } else { + bodyMap["text/html"] += Encoding(string(b), ct) } //body,_:=dec.Decode(string(b)) case *mail.AttachmentHeader: @@ -256,10 +256,10 @@ func GetMessage(server string, email string, password string, folder string, id } } - if bodyMap["text/html"]!=""{ - mailitem.Body =bodyMap["text/html"] - }else{ - mailitem.Body =bodyMap["text/plain"] + if bodyMap["text/html"] != "" { + mailitem.Body = bodyMap["text/html"] + } else { + mailitem.Body = bodyMap["text/plain"] } //log.Println(mailitem.Body) return mailitem diff --git a/imaptool.go b/tools/imaptool.go similarity index 99% rename from imaptool.go rename to tools/imaptool.go index 216467f..d382f9b 100644 --- a/imaptool.go +++ b/tools/imaptool.go @@ -1,4 +1,4 @@ -package main +package tools import ( "bufio" @@ -23,7 +23,7 @@ var ( folders map[int]string ) -func main() { +func testmain() { //获取参数中的数据 flag.StringVar(&server, "server", "", "imap服务地址(包含端口)")