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

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

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

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

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

@ -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
import "html/template"
type MailServer struct{
Server,Email,Password string
}
type IndexData struct {
Folders map[string]int
Mails interface{}

Loading…
Cancel
Save