邮件内容只获取html不获取text

pull/30/head 0.0.1
陶士涵 4 years ago
parent 7795a04129
commit 497dc08070

@ -8,15 +8,16 @@ import (
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"sync" "sync"
) )
const PAGE_SIZE = 20 const PageSize = 20
func main() { 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("/", index)
//邮件夹
http.HandleFunc("/list", list) http.HandleFunc("/list", list)
//登陆界面 //登陆界面
http.HandleFunc("/login", login) http.HandleFunc("/login", login)
@ -32,12 +33,11 @@ func index(w http.ResponseWriter, r *http.Request) {
return return
} }
auth := getCookie(r, "auth") mailServer:=tools.GetMailServerFromCookie(r)
if !strings.Contains(auth, "|") { if mailServer==nil {
http.Redirect(w, r, "/login", 302) http.Redirect(w, r, "/login", 302)
} else { } else {
authStrings := strings.Split(auth, "|") res := tools.CheckEmailPassword(mailServer.Server, mailServer.Email, mailServer.Password)
res := tools.CheckEmailPassword(authStrings[0], authStrings[1], authStrings[2])
if res { if res {
http.Redirect(w, r, "/list", 302) http.Redirect(w, r, "/list", 302)
} else { } else {
@ -64,8 +64,7 @@ func list(w http.ResponseWriter, r *http.Request) {
currentPage = 1 currentPage = 1
} }
auth := getCookie(r, "auth") mailServer:=tools.GetMailServerFromCookie(r)
authStrings := strings.Split(auth, "|")
render := new(tools.IndexData) render := new(tools.IndexData)
render.CurrentPage = currentPage render.CurrentPage = currentPage
@ -82,7 +81,7 @@ func list(w http.ResponseWriter, r *http.Request) {
wg.Add(2) wg.Add(2)
go func() { go func() {
defer wg.Done() 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.Folders = folders
render.Fid = fid render.Fid = fid
@ -108,7 +107,7 @@ func list(w http.ResponseWriter, r *http.Request) {
}() }()
go func() { go func() {
defer wg.Done() 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 render.MailPagelist = mails
}() }()
@ -133,21 +132,20 @@ func view(w http.ResponseWriter, r *http.Request) {
id = 0 id = 0
} }
auth := getCookie(r, "auth") mailServer:=tools.GetMailServerFromCookie(r)
authStrings := strings.Split(auth, "|")
var wg sync.WaitGroup var wg sync.WaitGroup
var render = new(tools.ViewData) var render = new(tools.ViewData)
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() 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.Folders = folders
render.Fid = fid render.Fid = fid
}() }()
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() 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.From = mail.From
render.To = mail.To render.To = mail.To
render.Subject = mail.Subject render.Subject = mail.Subject
@ -187,13 +185,4 @@ func login(w http.ResponseWriter, r *http.Request) {
//func authCookie(){ //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 ""
}

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

@ -92,7 +92,7 @@ func GetFolders(server string, email string, password string, folder string) map
break break
} }
} }
log.Println(folders) //log.Println(folders)
return folders return folders
} }
@ -131,7 +131,6 @@ func GetFolderMail(server string, email string, password string, folder string,
ret, _ = dec.DecodeHeader(msg.Envelope.Subject) ret, _ = dec.DecodeHeader(msg.Envelope.Subject)
} }
var mailitem = new(MailItem) var mailitem = new(MailItem)
log.Println(msg.SeqNum)
mailitem.Subject = ret mailitem.Subject = ret
mailitem.Id = msg.SeqNum 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 // Print some info about the message
header := mr.Header header := mr.Header
log.Println(header)
date, _ := header.Date() date, _ := header.Date()
log.Println("Date:", date) log.Println("Date:", date)
mailitem.Date = date.String() mailitem.Date = date.String()
@ -200,7 +198,6 @@ func GetMessage(server string, email string, password string, folder string, id
dec := GetDecoder() dec := GetDecoder()
if from, err := header.AddressList("From"); err == nil { if from, err := header.AddressList("From"); err == nil {
log.Println("From:", from)
for _, address := range from { for _, address := range from {
fromStr := address.String() fromStr := address.String()
temp, _ := dec.DecodeHeader(fromStr) temp, _ := dec.DecodeHeader(fromStr)
@ -208,6 +205,8 @@ func GetMessage(server string, email string, password string, folder string, id
} }
} }
mailitem.From = f mailitem.From = f
log.Println("From:", mailitem.From)
var t string var t string
if to, err := header.AddressList("To"); err == nil { if to, err := header.AddressList("To"); err == nil {
log.Println("To:", to) log.Println("To:", to)
@ -227,6 +226,10 @@ func GetMessage(server string, email string, password string, folder string, id
log.Println("Subject:", s) log.Println("Subject:", s)
mailitem.Subject = s mailitem.Subject = s
// Process each message's part // Process each message's part
var bodyMap=make(map[string]string)
bodyMap["text/plain"]= ""
bodyMap["text/html"]= ""
for { for {
p, err := mr.NextPart() p, err := mr.NextPart()
if err == io.EOF { if err == io.EOF {
@ -237,18 +240,28 @@ func GetMessage(server string, email string, password string, folder string, id
switch h := p.Header.(type) { switch h := p.Header.(type) {
case *mail.InlineHeader: case *mail.InlineHeader:
// This is the message's text (can be plain-text or HTML) // This is the message's text (can be plain-text or HTML)
b, _ := ioutil.ReadAll(p.Body) 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)) //body,_:=dec.Decode(string(b))
case *mail.AttachmentHeader: case *mail.AttachmentHeader:
// This is an attachment // This is an attachment
filename, _ := h.Filename() filename, _ := h.Filename()
log.Println("Got attachment: ", 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 return mailitem
} }
func GetDecoder() *mime.WordDecoder { func GetDecoder() *mime.WordDecoder {

@ -7,7 +7,6 @@ import (
"golang.org/x/text/encoding/unicode" "golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform" "golang.org/x/text/transform"
"io/ioutil" "io/ioutil"
"log"
"strings" "strings"
) )
@ -32,7 +31,6 @@ func Encoding(html string,ct string) string {
utf8Reader := transform.NewReader(r, e.NewDecoder()) utf8Reader := transform.NewReader(r, e.NewDecoder())
//将其他编码的reader转换为常用的utf8reader //将其他编码的reader转换为常用的utf8reader
all, _ := ioutil.ReadAll(utf8Reader) all, _ := ioutil.ReadAll(utf8Reader)
log.Println(string(all))
return string(all) return string(all)
} }
func DetermineEncoding(html string) (encoding.Encoding,string) { func DetermineEncoding(html string) (encoding.Encoding,string) {

@ -0,0 +1,14 @@
package tools
import (
"fmt"
"mime"
)
func main(){
dec := new(mime.WordDecoder)
header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= <eric@example.org>, =?utf-8?q?Ana=C3=AFs?= <anais@example.org>")
if err != nil {
panic(err)
}
fmt.Println(header)
}

@ -1,7 +1,9 @@
package tools package tools
import "html/template" import "html/template"
type MailServer struct{
Server,Email,Password string
}
type IndexData struct { type IndexData struct {
Folders map[string]int Folders map[string]int
Mails interface{} Mails interface{}

Loading…
Cancel
Save