From 497dc08070cf56f883c757990c1f3e1853a1ac46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Sat, 16 May 2020 17:55:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=82=AE=E4=BB=B6=E5=86=85=E5=AE=B9=E5=8F=AA?= =?UTF-8?q?=E8=8E=B7=E5=8F=96html=E4=B8=8D=E8=8E=B7=E5=8F=96text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.go | 39 ++++++++++++++------------------------- tools/cookie.go | 30 ++++++++++++++++++++++++++++++ tools/imap.go | 29 +++++++++++++++++++++-------- tools/stringutil.go | 2 -- tools/test.go | 14 ++++++++++++++ tools/types.go | 4 +++- 6 files changed, 82 insertions(+), 36 deletions(-) create mode 100644 tools/cookie.go create mode 100644 tools/test.go diff --git a/server.go b/server.go index 000d7ed..7fa0a7b 100644 --- a/server.go +++ b/server.go @@ -8,15 +8,16 @@ import ( "log" "net/http" "strconv" - "strings" "sync" ) -const PAGE_SIZE = 20 +const PageSize = 20 func main() { - log.Println("listen on 8080...") + log.Println("listen on 8080...\r\n访问:http://127.0.0.1:8080") + //根路径 http.HandleFunc("/", index) + //邮件夹 http.HandleFunc("/list", list) //登陆界面 http.HandleFunc("/login", login) @@ -32,12 +33,11 @@ func index(w http.ResponseWriter, r *http.Request) { return } - auth := getCookie(r, "auth") - if !strings.Contains(auth, "|") { + mailServer:=tools.GetMailServerFromCookie(r) + if mailServer==nil { http.Redirect(w, r, "/login", 302) } else { - authStrings := strings.Split(auth, "|") - res := tools.CheckEmailPassword(authStrings[0], authStrings[1], authStrings[2]) + res := tools.CheckEmailPassword(mailServer.Server, mailServer.Email, mailServer.Password) if res { http.Redirect(w, r, "/list", 302) } else { @@ -64,8 +64,7 @@ func list(w http.ResponseWriter, r *http.Request) { currentPage = 1 } - auth := getCookie(r, "auth") - authStrings := strings.Split(auth, "|") + mailServer:=tools.GetMailServerFromCookie(r) render := new(tools.IndexData) render.CurrentPage = currentPage @@ -82,7 +81,7 @@ func list(w http.ResponseWriter, r *http.Request) { wg.Add(2) go func() { defer wg.Done() - folders := tools.GetFolders(authStrings[0], authStrings[1], authStrings[2], fid) + folders := tools.GetFolders(mailServer.Server, mailServer.Email,mailServer.Password, fid) render.Folders = folders render.Fid = fid @@ -108,7 +107,7 @@ func list(w http.ResponseWriter, r *http.Request) { }() go func() { defer wg.Done() - mails := tools.GetFolderMail(authStrings[0], authStrings[1], authStrings[2], fid, currentPage, PAGE_SIZE) + mails := tools.GetFolderMail(mailServer.Server, mailServer.Email, mailServer.Password, fid, currentPage, PageSize) render.MailPagelist = mails }() @@ -133,21 +132,20 @@ func view(w http.ResponseWriter, r *http.Request) { id = 0 } - auth := getCookie(r, "auth") - authStrings := strings.Split(auth, "|") + mailServer:=tools.GetMailServerFromCookie(r) var wg sync.WaitGroup var render = new(tools.ViewData) wg.Add(1) go func() { defer wg.Done() - folders := tools.GetFolders(authStrings[0], authStrings[1], authStrings[2], fid) + folders := tools.GetFolders(mailServer.Server, mailServer.Email, mailServer.Password, fid) render.Folders = folders render.Fid = fid }() wg.Add(1) go func() { defer wg.Done() - mail := tools.GetMessage(authStrings[0], authStrings[1], authStrings[2], fid, id) + mail := tools.GetMessage(mailServer.Server, mailServer.Email, mailServer.Password, fid, id) render.From = mail.From render.To = mail.To render.Subject = mail.Subject @@ -187,13 +185,4 @@ func login(w http.ResponseWriter, r *http.Request) { //func authCookie(){ // //} -//获取cookie -func getCookie(r *http.Request, name string) string { - cookies := r.Cookies() - for _, cookie := range cookies { - if cookie.Name == name { - return cookie.Value - } - } - return "" -} + diff --git a/tools/cookie.go b/tools/cookie.go new file mode 100644 index 0000000..2d9befa --- /dev/null +++ b/tools/cookie.go @@ -0,0 +1,30 @@ +package tools + +import ( + "net/http" + "strings" +) + +func GetCookie(r *http.Request, name string) string { + cookies := r.Cookies() + for _, cookie := range cookies { + if cookie.Name == name { + return cookie.Value + } + } + return "" +} +func GetMailServerFromCookie(r *http.Request)*MailServer{ + auth := GetCookie(r, "auth") + if !strings.Contains(auth, "|") { + return nil + } + authStrings := strings.Split(auth, "|") + mailServer:=&MailServer{ + Server:authStrings[0], + Email: authStrings[1], + Password: authStrings[2], + } + return mailServer +} + diff --git a/tools/imap.go b/tools/imap.go index 7d19bac..2dcf9cb 100644 --- a/tools/imap.go +++ b/tools/imap.go @@ -92,7 +92,7 @@ func GetFolders(server string, email string, password string, folder string) map break } } - log.Println(folders) + //log.Println(folders) return folders } @@ -131,7 +131,6 @@ func GetFolderMail(server string, email string, password string, folder string, ret, _ = dec.DecodeHeader(msg.Envelope.Subject) } var mailitem = new(MailItem) - log.Println(msg.SeqNum) mailitem.Subject = ret mailitem.Id = msg.SeqNum @@ -191,7 +190,6 @@ func GetMessage(server string, email string, password string, folder string, id // Print some info about the message header := mr.Header - log.Println(header) date, _ := header.Date() log.Println("Date:", date) mailitem.Date = date.String() @@ -200,7 +198,6 @@ func GetMessage(server string, email string, password string, folder string, id dec := GetDecoder() if from, err := header.AddressList("From"); err == nil { - log.Println("From:", from) for _, address := range from { fromStr := address.String() temp, _ := dec.DecodeHeader(fromStr) @@ -208,6 +205,8 @@ func GetMessage(server string, email string, password string, folder string, id } } mailitem.From = f + log.Println("From:", mailitem.From) + var t string if to, err := header.AddressList("To"); err == nil { log.Println("To:", to) @@ -227,6 +226,10 @@ 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"]= "" + for { p, err := mr.NextPart() if err == io.EOF { @@ -237,18 +240,28 @@ func GetMessage(server string, email string, password string, folder string, id switch h := p.Header.(type) { case *mail.InlineHeader: // This is the message's text (can be plain-text or HTML) + b, _ := ioutil.ReadAll(p.Body) - mailitem.Body += string(b) + 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: // This is an attachment filename, _ := h.Filename() log.Println("Got attachment: ", filename) } - ct,_,_:=header.ContentType() - log.Println(ct) - mailitem.Body = Encoding(mailitem.Body,ct) + + } + if bodyMap["text/html"]!=""{ + mailitem.Body =bodyMap["text/html"] + }else{ + mailitem.Body =bodyMap["text/plain"] } + //log.Println(mailitem.Body) return mailitem } func GetDecoder() *mime.WordDecoder { diff --git a/tools/stringutil.go b/tools/stringutil.go index 6183a41..003dce9 100644 --- a/tools/stringutil.go +++ b/tools/stringutil.go @@ -7,7 +7,6 @@ import ( "golang.org/x/text/encoding/unicode" "golang.org/x/text/transform" "io/ioutil" - "log" "strings" ) @@ -32,7 +31,6 @@ func Encoding(html string,ct string) string { utf8Reader := transform.NewReader(r, e.NewDecoder()) //将其他编码的reader转换为常用的utf8reader all, _ := ioutil.ReadAll(utf8Reader) - log.Println(string(all)) return string(all) } func DetermineEncoding(html string) (encoding.Encoding,string) { diff --git a/tools/test.go b/tools/test.go new file mode 100644 index 0000000..5483335 --- /dev/null +++ b/tools/test.go @@ -0,0 +1,14 @@ +package tools + +import ( + "fmt" + "mime" +) +func main(){ + dec := new(mime.WordDecoder) + header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= , =?utf-8?q?Ana=C3=AFs?= ") + if err != nil { + panic(err) + } + fmt.Println(header) +} \ No newline at end of file diff --git a/tools/types.go b/tools/types.go index 8e0e593..bae6e02 100644 --- a/tools/types.go +++ b/tools/types.go @@ -1,7 +1,9 @@ package tools import "html/template" - +type MailServer struct{ + Server,Email,Password string +} type IndexData struct { Folders map[string]int Mails interface{}