diff --git a/server.go b/server.go index 0ea2d60..df435e6 100644 --- a/server.go +++ b/server.go @@ -126,6 +126,13 @@ func view(w http.ResponseWriter, r *http.Request) { }else{ fid="INBOX" } + var id uint32 + if len(values["id"])!=0{ + i,_:=strconv.Atoi(values["id"][0]) + id=uint32(i) + }else{ + id=0 + } auth := getCookie(r, "auth") authStrings := strings.Split(auth, "|") @@ -138,6 +145,15 @@ func view(w http.ResponseWriter, r *http.Request) { 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) + render.From=mail.From + render.To=mail.To + render.Subject=mail.Subject + render.Date=mail.Date + }() wg.Wait() tmpl.RenderView(w,render) } diff --git a/tmpl/view.go b/tmpl/view.go index 0ffef8c..57e07bf 100644 --- a/tmpl/view.go +++ b/tmpl/view.go @@ -43,19 +43,27 @@ func RenderView(w http.ResponseWriter,render interface{}){ - - + + - + + + + + + + + + -
主题:Mark日期:{{.Date}}
发件人:Mark{{.From}}
收件人:{{.To}}
主题:{{.Subject}}
内容: Jacob
+ diff --git a/tools/imap.go b/tools/imap.go index 4aebc93..e3eb0cb 100644 --- a/tools/imap.go +++ b/tools/imap.go @@ -6,6 +6,7 @@ import ( "github.com/axgle/mahonia" "github.com/emersion/go-imap" "github.com/emersion/go-imap/client" + "github.com/emersion/go-message/mail" "io" "io/ioutil" "log" @@ -118,6 +119,126 @@ func GetFolderMail(server string, email string, password string,folder string,cu }() var mailPagelist=new(MailPageList) + dec :=GetDecoder() + + for msg:=range messages{ + + ret,err:=dec.Decode(msg.Envelope.Subject) + if err!=nil{ + ret,_=dec.DecodeHeader(msg.Envelope.Subject) + } + var mailitem =new(MailItem) + log.Println(msg.SeqNum) + + mailitem.Subject=ret + mailitem.Id=msg.SeqNum + mailitem.Fid=folder + mailPagelist.MailItems=append(mailPagelist.MailItems,mailitem) + } + return mailPagelist.MailItems +} +func GetMessage(server string, email string, password string,folder string,id uint32)*MailItem{ + var c *client.Client + //defer c.Logout() + c=connect(server,email,password) + if c==nil{ + //return nil + } + // Select INBOX + mbox, err := c.Select(folder, false) + if err != nil { + log.Fatal(err) + } + + // Get the last message + if mbox.Messages == 0 { + log.Fatal("No message in mailbox") + } + seqSet := new(imap.SeqSet) + seqSet.AddNum(id) + + // Get the whole message body + var section imap.BodySectionName + items := []imap.FetchItem{section.FetchItem()} + + messages := make(chan *imap.Message, 1) + go func() { + if err := c.Fetch(seqSet, items, messages); err != nil { + log.Fatal(err) + } + }() + + msg := <-messages + if msg == nil { + log.Fatal("Server didn't returned message") + } + + r := msg.GetBody(§ion) + if r == nil { + log.Fatal("Server didn't returned message body") + } + + // Create a new mail reader + mr, err := mail.CreateReader(r) + if err != nil { + log.Fatal(err) + } + var mailitem =new(MailItem) + + // Print some info about the message + header := mr.Header + if date, err := header.Date(); err == nil { + log.Println("Date:", date) + mailitem.Date=date.String() + } + var f string + if from, err := header.AddressList("From"); err == nil { + log.Println("From:", from) + for _,address:=range from{ + f+=" "+address.String() + } + } + mailitem.From=f + var t string + if to, err := header.AddressList("To"); err == nil { + log.Println("To:", to) + for _,address:=range to{ + t+=" "+address.String() + } + } + mailitem.To=t + dec:=GetDecoder() + if subject, err := header.Subject(); err == nil { + s,err:=dec.Decode(subject) + if err!=nil{ + s,_=dec.DecodeHeader(subject) + } + log.Println("Subject:", s) + mailitem.Subject=s + } + // Process each message's part + //for { + // p, err := mr.NextPart() + // if err == io.EOF { + // break + // } else if err != nil { + // log.Fatal(err) + // } + // + // 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) + // log.Println("Got text: ", string(b)) + // case *mail.AttachmentHeader: + // // This is an attachment + // filename, _ := h.Filename() + // log.Println("Got attachment: ", filename) + // } + //} + return mailitem +} +func GetDecoder()*mime.WordDecoder{ dec :=new(mime.WordDecoder) dec.CharsetReader= func(charset string, input io.Reader) (io.Reader, error) { switch charset { @@ -165,21 +286,7 @@ func GetFolderMail(server string, email string, password string,folder string,cu } } - for msg:=range messages{ - - ret,err:=dec.Decode(msg.Envelope.Subject) - if err!=nil{ - ret,_=dec.DecodeHeader(msg.Envelope.Subject) - } - var mailitem =new(MailItem) - log.Println(msg.SeqNum) - - mailitem.Subject=ret - mailitem.Id=msg.SeqNum - mailitem.Fid=folder - mailPagelist.MailItems=append(mailPagelist.MailItems,mailitem) - } - return mailPagelist.MailItems + return dec } // 任意编码转特定编码 func ConvertToStr(src string, srcCode string, tagCode string) string { diff --git a/tools/types.go b/tools/types.go index 90ca69e..8c3cdf3 100644 --- a/tools/types.go +++ b/tools/types.go @@ -14,12 +14,16 @@ type IndexData struct { } type ViewData struct { Folders map[string]int - Fid string + MailItem } type MailItem struct{ Subject string - Id uint32 Fid string + Id uint32 + From string + To string + Body string + Date string } type MailPageList struct{ MailItems []*MailItem