first commit
This commit is contained in:
commit
494940afc9
87 files changed
+9589
No files matched your search
@@ -0,0 +1,50 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cast"
|
||||
"net/http"
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
"pmail/services/attachments"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetAttachments(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
urlInfos := strings.Split(req.RequestURI, "/")
|
||||
if len(urlInfos) != 4 {
|
||||
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
emailId := cast.ToInt(urlInfos[2])
|
||||
cid := urlInfos[3]
|
||||
|
||||
contentType, content := attachments.GetAttachments(ctx, emailId, cid)
|
||||
|
||||
if len(content) == 0 {
|
||||
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Write(content)
|
||||
}
|
||||
|
||||
func Download(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
urlInfos := strings.Split(req.RequestURI, "/")
|
||||
if len(urlInfos) != 5 {
|
||||
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
emailId := cast.ToInt(urlInfos[3])
|
||||
index := cast.ToInt(urlInfos[4])
|
||||
|
||||
fileName, content := attachments.GetAttachmentsByIndex(ctx, emailId, index)
|
||||
|
||||
if len(content) == 0 {
|
||||
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
w.Header().Set("ContentType", "application/octet-stream")
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%s", fileName))
|
||||
w.Write(content)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"pmail/dto"
|
||||
)
|
||||
|
||||
type HandlerFunc func(*dto.Context, http.ResponseWriter, *http.Request)
|
||||
@@ -0,0 +1,49 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
"pmail/services/auth"
|
||||
"pmail/services/detail"
|
||||
)
|
||||
|
||||
type emailDetailRequest struct {
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
func EmailDetail(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
reqBytes, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("%+v", err)
|
||||
}
|
||||
var retData emailDetailRequest
|
||||
err = json.Unmarshal(reqBytes, &retData)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("%+v", err)
|
||||
}
|
||||
|
||||
if retData.ID <= 0 {
|
||||
response.NewErrorResponse(response.ParamsError, "ID错误", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
email, err := detail.GetEmailDetail(ctx, retData.ID, true)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否有权限
|
||||
hasAuth := auth.HasAuth(ctx, email)
|
||||
if !hasAuth {
|
||||
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
response.NewSuccessResponse(email).FPrint(w)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cast"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
"pmail/services/list"
|
||||
)
|
||||
|
||||
type emailListResponse struct {
|
||||
CurrentPage int `json:"current_page"`
|
||||
TotalPage int `json:"total_page"`
|
||||
List []*emilItem `json:"list"`
|
||||
}
|
||||
|
||||
type emilItem struct {
|
||||
ID int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Desc string `json:"desc"`
|
||||
Datetime string `json:"datetime"`
|
||||
IsRead bool `json:"is_read"`
|
||||
Sender User `json:"sender"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Name string `json:"Name"`
|
||||
EmailAddress string `json:"EmailAddress"`
|
||||
}
|
||||
|
||||
type emailRequest struct {
|
||||
Keyword string `json:"keyword"`
|
||||
Tag string `json:"tag"`
|
||||
CurrentPage int `json:"current_page"`
|
||||
PageSize int `json:"page_size"`
|
||||
}
|
||||
|
||||
func EmailList(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
var lst []*emilItem
|
||||
reqBytes, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("%+v", err)
|
||||
}
|
||||
var retData emailRequest
|
||||
err = json.Unmarshal(reqBytes, &retData)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("%+v", err)
|
||||
}
|
||||
|
||||
offset := 0
|
||||
if retData.CurrentPage >= 1 {
|
||||
offset = (retData.CurrentPage - 1) * retData.PageSize
|
||||
}
|
||||
|
||||
if retData.PageSize == 0 {
|
||||
retData.PageSize = 15
|
||||
}
|
||||
|
||||
emailList, total := list.GetEmailList(ctx, retData.Tag, retData.Keyword, offset, retData.PageSize)
|
||||
|
||||
for _, email := range emailList {
|
||||
var sender User
|
||||
_ = json.Unmarshal([]byte(email.Sender), &sender)
|
||||
|
||||
lst = append(lst, &emilItem{
|
||||
ID: email.Id,
|
||||
Title: email.Subject,
|
||||
Desc: email.Text.String,
|
||||
Datetime: email.SendDate.Format("2006-01-02 15:04:05"),
|
||||
IsRead: email.IsRead == 1,
|
||||
Sender: sender,
|
||||
})
|
||||
}
|
||||
|
||||
ret := emailListResponse{
|
||||
CurrentPage: retData.CurrentPage,
|
||||
TotalPage: cast.ToInt(math.Ceil(cast.ToFloat64(total) / cast.ToFloat64(retData.PageSize))),
|
||||
List: lst,
|
||||
}
|
||||
response.NewSuccessResponse(ret).FPrint(w)
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"pmail/config"
|
||||
"pmail/dto"
|
||||
"pmail/dto/parsemail"
|
||||
"pmail/dto/response"
|
||||
"pmail/hooks"
|
||||
"pmail/i18n"
|
||||
"pmail/mysql"
|
||||
"pmail/smtp_server"
|
||||
"pmail/utils/async"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type sendRequest struct {
|
||||
ReplyTo []user `json:"reply_to"`
|
||||
From user `json:"from"`
|
||||
To []user `json:"to"`
|
||||
Bcc []user `json:"bcc"`
|
||||
Cc []user `json:"cc"`
|
||||
Subject string `json:"subject"`
|
||||
Text string `json:"text"` // Plaintext message (optional)
|
||||
HTML string `json:"html"` // Html message (optional)
|
||||
Sender user `json:"sender"` // override From as SMTP envelope sender (optional)
|
||||
ReadReceipt []string `json:"read_receipt"`
|
||||
Attachments []attachment `json:"attrs"`
|
||||
}
|
||||
|
||||
type user struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
type attachment struct {
|
||||
Name string `json:"name"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
func Send(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
reqBytes, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("%+v", err)
|
||||
response.NewErrorResponse(response.ParamsError, "params error", err.Error()).FPrint(w)
|
||||
return
|
||||
}
|
||||
log.WithContext(ctx).Infof("发送邮件")
|
||||
|
||||
var reqData sendRequest
|
||||
err = json.Unmarshal(reqBytes, &reqData)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("%+v", err)
|
||||
response.NewErrorResponse(response.ParamsError, "params error", err.Error()).FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData.From.Email == "" && reqData.From.Name != "" {
|
||||
reqData.From.Email = reqData.From.Name + "@" + config.Instance.Domain
|
||||
}
|
||||
|
||||
if reqData.From.Email == "" {
|
||||
response.NewErrorResponse(response.ParamsError, "发件人必填", "发件人必填").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData.Subject == "" {
|
||||
response.NewErrorResponse(response.ParamsError, "邮件标题必填", "邮件标题必填").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if len(reqData.To) <= 0 {
|
||||
response.NewErrorResponse(response.ParamsError, "收件人必填", "收件人必填").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
e := &parsemail.Email{}
|
||||
|
||||
for _, to := range reqData.To {
|
||||
e.To = append(e.To, &parsemail.User{
|
||||
Name: to.Name,
|
||||
EmailAddress: to.Email,
|
||||
})
|
||||
}
|
||||
|
||||
for _, bcc := range reqData.Bcc {
|
||||
e.Bcc = append(e.Bcc, &parsemail.User{
|
||||
Name: bcc.Name,
|
||||
EmailAddress: bcc.Email,
|
||||
})
|
||||
}
|
||||
|
||||
for _, cc := range reqData.Cc {
|
||||
e.Cc = append(e.Cc, &parsemail.User{
|
||||
Name: cc.Name,
|
||||
EmailAddress: cc.Email,
|
||||
})
|
||||
}
|
||||
|
||||
e.From = &parsemail.User{
|
||||
Name: reqData.From.Name,
|
||||
EmailAddress: reqData.From.Email,
|
||||
}
|
||||
e.Text = []byte(reqData.Text)
|
||||
e.HTML = []byte(reqData.HTML)
|
||||
e.Subject = reqData.Subject
|
||||
for _, att := range reqData.Attachments {
|
||||
att.Data = strings.TrimPrefix(att.Data, "data:")
|
||||
infos := strings.Split(att.Data, ";")
|
||||
contentType := infos[0]
|
||||
content := strings.TrimPrefix(infos[1], "base64,")
|
||||
decoded, err := base64.StdEncoding.DecodeString(content)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("附件解码错误!%v", err)
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "att_err"), err.Error()).FPrint(w)
|
||||
return
|
||||
}
|
||||
e.Attachments = append(e.Attachments, &parsemail.Attachment{
|
||||
Filename: att.Name,
|
||||
ContentType: contentType,
|
||||
Content: decoded,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
for _, hook := range hooks.HookList {
|
||||
if hook == nil {
|
||||
continue
|
||||
}
|
||||
async.New(ctx).Process(func() {
|
||||
hook.SendBefore(ctx, e)
|
||||
})
|
||||
}
|
||||
|
||||
// 邮件落库
|
||||
sql := "INSERT INTO email (type,subject, reply_to, from_name, from_address, `to`, bcc, cc, text, html, sender, attachments,spf_check, dkim_check, create_time,send_user_id,error) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
sqlRes, sqlerr := mysql.Instance.Exec(mysql.WithContext(ctx, sql),
|
||||
1,
|
||||
e.Subject,
|
||||
json2string(e.ReplyTo),
|
||||
e.From.Name,
|
||||
e.From.EmailAddress,
|
||||
json2string(e.To),
|
||||
json2string(e.Bcc),
|
||||
json2string(e.Cc),
|
||||
e.Text,
|
||||
e.HTML,
|
||||
json2string(e.Sender),
|
||||
json2string(e.Attachments),
|
||||
1,
|
||||
1,
|
||||
time.Now(),
|
||||
ctx.UserInfo.ID,
|
||||
"",
|
||||
)
|
||||
emailId, _ := sqlRes.LastInsertId()
|
||||
|
||||
if sqlerr != nil || emailId <= 0 {
|
||||
log.Println("mysql insert error:", err.Error())
|
||||
response.NewErrorResponse(response.ServerError, i18n.GetText(ctx.Lang, "send_fail"), err.Error()).FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
async.New(ctx).Process(func() {
|
||||
errMsg := ""
|
||||
err, sendErr := smtp_server.Send(ctx, e)
|
||||
|
||||
for _, hook := range hooks.HookList {
|
||||
if hook == nil {
|
||||
continue
|
||||
}
|
||||
async.New(ctx).Process(func() {
|
||||
hook.SendAfter(ctx, e, sendErr)
|
||||
})
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
errMsg = err.Error()
|
||||
_, err := mysql.Instance.Exec(mysql.WithContext(ctx, "update email set status =2 ,error=? where id = ? "), errMsg, emailId)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("sql Error :%+v", err)
|
||||
}
|
||||
} else {
|
||||
_, err := mysql.Instance.Exec(mysql.WithContext(ctx, "update email set status =1 where id = ? "), emailId)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("sql Error :%+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
response.NewSuccessResponse(i18n.GetText(ctx.Lang, "succ")).FPrint(w)
|
||||
}
|
||||
|
||||
func json2string(d any) string {
|
||||
by, _ := json.Marshal(d)
|
||||
return string(by)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
"pmail/i18n"
|
||||
)
|
||||
|
||||
type groupItem struct {
|
||||
Label string `json:"label"`
|
||||
Tag string `json:"tag"`
|
||||
Children []*groupItem `json:"children"`
|
||||
}
|
||||
|
||||
func GetUserGroup(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
retData := []*groupItem{
|
||||
{
|
||||
Label: i18n.GetText(ctx.Lang, "all_email"),
|
||||
Children: []*groupItem{
|
||||
{
|
||||
Label: i18n.GetText(ctx.Lang, "inbox"),
|
||||
Tag: dto.SearchTag{Type: 0, Status: -1}.ToString(),
|
||||
},
|
||||
{
|
||||
Label: i18n.GetText(ctx.Lang, "outbox"),
|
||||
Tag: dto.SearchTag{Type: 1, Status: 1}.ToString(),
|
||||
},
|
||||
{
|
||||
Label: i18n.GetText(ctx.Lang, "sketch"),
|
||||
Tag: dto.SearchTag{Type: 1, Status: 0}.ToString(),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
response.NewSuccessResponse(retData).FPrint(w)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
"pmail/i18n"
|
||||
"pmail/models"
|
||||
"pmail/mysql"
|
||||
"pmail/session"
|
||||
)
|
||||
|
||||
type loginRequest struct {
|
||||
Account string `json:"account"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func Login(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
reqBytes, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
var retData loginRequest
|
||||
err = json.Unmarshal(reqBytes, &retData)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
var user models.User
|
||||
|
||||
encodePwd := md5Encode(md5Encode(retData.Password+"pmail") + "pmail2023")
|
||||
|
||||
err = mysql.Instance.Get(&user, mysql.WithContext(ctx, "select * from user where account =? and password =?"),
|
||||
retData.Account, encodePwd)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
if user.ID != 0 {
|
||||
userStr, _ := json.Marshal(user)
|
||||
session.Instance.Put(req.Context(), "user", string(userStr))
|
||||
response.NewSuccessResponse("").FPrint(w)
|
||||
} else {
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "aperror"), "").FPrint(w)
|
||||
}
|
||||
}
|
||||
|
||||
func md5Encode(str string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(str))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
)
|
||||
|
||||
func Ping(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
response.NewSuccessResponse("pong").FPrint(w)
|
||||
log.WithContext(ctx).Info("pong")
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"net/http"
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
"pmail/i18n"
|
||||
"pmail/mysql"
|
||||
)
|
||||
|
||||
type modifyPasswordRequest struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func ModifyPassword(ctx *dto.Context, w http.ResponseWriter, req *http.Request) {
|
||||
reqBytes, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
var retData modifyPasswordRequest
|
||||
err = json.Unmarshal(reqBytes, &retData)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
if retData.Password != "" {
|
||||
encodePwd := md5Encode(md5Encode(retData.Password+"pmail") + "pmail2023")
|
||||
|
||||
_, err := mysql.Instance.Exec(mysql.WithContext(ctx, "update user set password = ? where id =?"), encodePwd, ctx.UserInfo.ID)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, i18n.GetText(ctx.Lang, "unknowError"), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
response.NewSuccessResponse(i18n.GetText(ctx.Lang, "succ")).FPrint(w)
|
||||
}
|
||||
Reference in new issue
Block a user