新增详情页读取和代码编码器优化

pull/30/head
taoshihan1991 5 years ago
parent c58b956a3c
commit 413cbc07ce

@ -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)
}

@ -43,12 +43,20 @@ func RenderView(w http.ResponseWriter,render interface{}){
<table class="table table-hover">
<tbody>
<tr>
<th scope="row">:</th>
<td>Mark</td>
<th scope="row">:</th>
<td>{{.Date}}</td>
</tr>
<tr>
<th scope="row">:</th>
<td>Mark</td>
<td>{{.From}}</td>
</tr>
<tr>
<th scope="row">:</th>
<td>{{.To}}</td>
</tr>
<tr>
<th scope="row">:</th>
<td>{{.Subject}}</td>
</tr>
<tr>
<th scope="row">:</th>

@ -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(&section)
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 {

@ -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

Loading…
Cancel
Save