From bd93b64dbb8c9baf8ab73cc24b1a84df0014224e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Sun, 17 May 2020 07:44:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=B2=A1=E6=9C=89mailreader?= =?UTF-8?q?=E6=97=B6=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 8 +++----- tools/imap.go | 30 +++++++++++++++--------------- imaptool.go => tools/imaptool.go | 4 ++-- 3 files changed, 20 insertions(+), 22 deletions(-) rename imaptool.go => tools/imaptool.go (99%) 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服务地址(包含端口)")