读取邮件列表

pull/30/head
unknown 5 years ago
parent c162829b95
commit 54a043dd1c

@ -9,33 +9,56 @@ import (
"strings" "strings"
) )
type IndexData struct {
Folders map[string]int
Mails interface{}
}
func main() { func main() {
log.Println("listen on 8080...") log.Println("listen on 8080...")
http.HandleFunc("/", index) http.HandleFunc("/", index)
http.HandleFunc("/list", list)
//登陆界面 //登陆界面
http.HandleFunc("/login", login) http.HandleFunc("/login", login)
//监听端口 //监听端口
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }
//输出首页 //首页跳转
func index(w http.ResponseWriter, r *http.Request) { func index(w http.ResponseWriter, r *http.Request) {
if r.URL.RequestURI()=="/favicon.ico"{
return
}
auth := getCookie(r, "auth") auth := getCookie(r, "auth")
if !strings.Contains(auth,"|"){ if !strings.Contains(auth, "|") {
http.Redirect(w, r, "/login", 302) http.Redirect(w, r, "/login", 302)
}else { } else {
authStrings := strings.Split(auth, "|") authStrings := strings.Split(auth, "|")
res := tools.CheckEmailPassword(authStrings[0], authStrings[1], authStrings[2]) res := tools.CheckEmailPassword(authStrings[0], authStrings[1], authStrings[2])
if res{ if res {
folders := tools.GetFolders(authStrings[0], authStrings[1], authStrings[2]) http.Redirect(w, r, "/list", 302)
t, _ := template.ParseFiles("./tmpl/index.html") } else {
t.Execute(w, folders)
}else{
http.Redirect(w, r, "/login", 302) http.Redirect(w, r, "/login", 302)
} }
} }
} }
//输出列表
func list(w http.ResponseWriter, r *http.Request) {
auth := getCookie(r, "auth")
authStrings := strings.Split(auth, "|")
render := new(IndexData)
folders := tools.GetFolders(authStrings[0], authStrings[1], authStrings[2])
render.Folders = folders
mails := tools.GetFolderMail(authStrings[0], authStrings[1], authStrings[2], "INBOX", 10)
render.Mails = mails
t, _ := template.ParseFiles("./tmpl/index.html")
t.Execute(w, render)
}
//登陆界面 //登陆界面
func login(w http.ResponseWriter, r *http.Request) { func login(w http.ResponseWriter, r *http.Request) {
email := r.PostFormValue("email") email := r.PostFormValue("email")

@ -22,10 +22,10 @@
<div class="col-md-3"> <div class="col-md-3">
<ul class="list-group"> <ul class="list-group">
<li class="list-group-item active">邮件夹</li> <li class="list-group-item active">邮件夹</li>
{{ range $key, $value := .}} {{ range $key, $value := .Folders}}
<li class="list-group-item d-flex justify-content-between align-items-center"> <li class="list-group-item d-flex justify-content-between align-items-center">
{{$key}} {{$key}}
<span class="badge badge-primary badge-pill">{{$value}}</span> {{if ne $value 0 }}<span class="badge badge-primary badge-pill">{{$value}}</span>{{end}}
</li> </li>
{{end}} {{end}}
</ul> </ul>
@ -33,9 +33,9 @@
<div class="col-md-9"> <div class="col-md-9">
<ul class="list-group"> <ul class="list-group">
<li class="list-group-item active">邮件列表</li> <li class="list-group-item active">邮件列表</li>
{{ range $key, $value := .}} {{ range $key, $value := .Mails}}
<li class="list-group-item d-flex justify-content-between align-items-center"> <li class="list-group-item d-flex justify-content-between align-items-center">
{{$key}} {{$value}}
</li> </li>
{{end}} {{end}}
</ul> </ul>

@ -7,7 +7,6 @@ import (
"log" "log"
"strconv" "strconv"
"strings" "strings"
"sync"
) )
//验证邮箱密码 //验证邮箱密码
func CheckEmailPassword(server string, email string, password string) bool { func CheckEmailPassword(server string, email string, password string) bool {
@ -73,21 +72,45 @@ func GetFolders(server string, email string, password string)map[string]int{
// 存储邮件夹 // 存储邮件夹
var folders =make(map[string]int) var folders =make(map[string]int)
for m := range mailboxes { for m := range mailboxes {
folders[m.Name]=1 folders[m.Name]=0
} }
var wg sync.WaitGroup for m,_ := range folders {
var k string if m=="INBOX" {
for k,_=range folders{ mbox, _ := c.Select(m, true)
//wg.Add(1) if mbox != nil {
//go func(k string) { folders[m] = int(mbox.Messages)
mbox, _ := c.Select(k, false)
if mbox!=nil{
log.Println(k,mbox.Messages)
folders[k]=int(mbox.Messages)
} }
//wg.Done() break
//}(k) }
} }
wg.Wait() log.Println(folders)
return folders return folders
}
//获取邮件夹邮件
func GetFolderMail(server string, email string, password string,folder string,pagesize int)[]string{
var c *client.Client
//defer c.Logout()
c=connect(server,email,password)
if c==nil{
return nil
}
mbox, _ := c.Select(folder, true)
to := mbox.Messages
from := to-uint32(pagesize)
seqset := new(imap.SeqSet)
seqset.AddRange(from, to)
messages := make(chan *imap.Message, pagesize)
done := make(chan error, 1)
go func() {
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
}()
log.Println(111)
var res []string
for msg := range messages {
res=append(res,msg.Envelope.Subject)
}
return res
} }
Loading…
Cancel
Save