首页展示邮件夹列表

pull/30/head
unknown 4 years ago
parent c105ef7f1f
commit 830fffa353

@ -6,6 +6,7 @@ import (
"html/template"
"log"
"net/http"
"strings"
)
func main() {
@ -20,11 +21,19 @@ func main() {
//输出首页
func index(w http.ResponseWriter, r *http.Request) {
auth := getCookie(r, "auth")
if auth == "" {
if !strings.Contains(auth,"|"){
http.Redirect(w, r, "/login", 302)
}else {
authStrings := strings.Split(auth, "|")
res := tools.CheckEmailPassword(authStrings[0], authStrings[1], authStrings[2])
if res{
folders := tools.GetFolders(authStrings[0], authStrings[1], authStrings[2])
t, _ := template.ParseFiles("./tmpl/index.html")
t.Execute(w, folders)
}else{
http.Redirect(w, r, "/login", 302)
}
}
t, _ := template.ParseFiles("./tmpl/index.html")
t.Execute(w, nil)
}
//登陆界面
@ -40,7 +49,7 @@ func login(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("./tmpl/login.html")
t.Execute(w, errStr)
} else {
auth := fmt.Sprintf("%s:%s:%s", server, email, password)
auth := fmt.Sprintf("%s|%s|%s", server, email, password)
cookie := http.Cookie{
Name: "auth",
Value: auth,

@ -1 +1,34 @@
<h1>hello</h1>
<!doctype html>
<html lang="cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Jekyll v3.8.6">
<title>邮箱IMAP-首页</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<style>
</style>
</head>
<body>
<div class="row">
<div class="col-md-4">
<ul class="list-group">
{{ range $key, $value := .}}
<li class="list-group-item d-flex justify-content-between align-items-center">
{{$value}}
<span class="badge badge-primary badge-pill">14</span>
</li>
{{end}}}
</ul>
</div>
<div class="col-md-8">.col-md-8</div>
</div>
</body>
</html>

@ -81,7 +81,7 @@
<input type="email" id="inputEmail" name="email" class="form-control" placeholder="邮箱地址" required autofocus>
<label for="inputPassword" class="sr-only">密码:</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="密码" required>
{{if .}}<div><code>{{.}}</code></div>{{end}}
{{if .}}<div class="alert alert-danger" role="alert">{{.}}</div>{{end}}
<button class="btn btn-lg btn-primary btn-block" type="submit">登陆</button>
<p class="mt-5 mb-3 text-muted">&copy; 2020</p>
</form>

@ -2,22 +2,42 @@ package tools
import (
"fmt"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"strconv"
"strings"
)
//验证邮箱密码
func CheckEmailPassword(server string, email string, password string) bool {
if !strings.Contains(server, ":") {
return false
}
var c *client.Client
serverSlice := strings.Split(server, ":")
port, _ := strconv.Atoi(serverSlice[1])
if port != 993 && port != 143 {
return false
}
// 不要忘了退出
//defer c.Logout()
// 登陆
c=connect(server,email,password)
if c==nil{
return false
}
return true
}
//获取连接
func connect(server string, email string, password string)*client.Client{
var c *client.Client
var err error
serverSlice := strings.Split(server, ":")
uri := serverSlice[0]
port, _ := strconv.Atoi(serverSlice[1])
if port != 993 && port != 143 {
return false
return nil
}
if port == 993 {
c, err = client.DialTLS(fmt.Sprintf("%s:%d", uri, port), nil)
@ -25,15 +45,33 @@ func CheckEmailPassword(server string, email string, password string) bool {
c, err = client.Dial(fmt.Sprintf("%s:%d", uri, port))
}
if err != nil {
return false
return nil
}
// 不要忘了退出
defer c.Logout()
// 登陆
if err := c.Login(email, password); err != nil {
return false
return nil
}
return true
return c
}
//获取邮件夹
func GetFolders(server string, email string, password string)[]string{
var c *client.Client
//defer c.Logout()
c=connect(server,email,password)
if c==nil{
return nil
}
// 列邮箱
mailboxes := make(chan *imap.MailboxInfo, 10)
done := make(chan error, 1)
go func() {
done <- c.List("", "*", mailboxes)
}()
// 存储邮件夹
var folders []string
for m := range mailboxes {
folders=append(folders,m.Name)
}
return folders
}
Loading…
Cancel
Save