Quellcode durchsuchen

Add sent folder

- Put sent emails to Sent folder
- Extend savemail interface, add 'read' flag
Alexey Edelev vor 5 Jahren
Ursprung
Commit
176367f0f1
4 geänderte Dateien mit 30 neuen und 11 gelöschten Zeilen
  1. 1 0
      common/folders.go
  2. 3 2
      db/db.go
  3. 2 2
      scanner/mailscanner.go
  4. 24 7
      web/mailbox.go

+ 1 - 0
common/folders.go

@@ -29,4 +29,5 @@ const (
 	Inbox = "Inbox"
 	Trash = "Trash"
 	Spam  = "Spam"
+	Sent  = "Sent"
 )

+ 3 - 2
db/db.go

@@ -302,7 +302,7 @@ func (s *Storage) CleanupTokens(user string) {
 	_, err = s.tokensCollection.UpdateOne(context.Background(), bson.M{"user": user}, bson.M{"$set": bson.M{"token": tokensToKeep}})
 	return
 }
-func (s *Storage) SaveMail(email, folder string, m *common.Mail) error {
+func (s *Storage) SaveMail(email, folder string, m *common.Mail, read bool) error {
 	result := &struct {
 		User string
 	}{}
@@ -320,7 +320,7 @@ func (s *Storage) SaveMail(email, folder string, m *common.Mail) error {
 		Email:  email,
 		Mail:   m,
 		Folder: folder,
-		Read:   false,
+		Read:   read,
 		Trash:  false,
 	}, options.InsertOne().SetBypassDocumentValidation(true))
 	return nil
@@ -540,6 +540,7 @@ func (s *Storage) GetAllEmails() (emails []string, err error) {
 func (s *Storage) GetFolders(email string) (folders []*common.Folder) {
 	folders = []*common.Folder{
 		&common.Folder{Name: common.Inbox, Custom: false},
+		&common.Folder{Name: common.Sent, Custom: false},
 		&common.Folder{Name: common.Trash, Custom: false},
 		&common.Folder{Name: common.Spam, Custom: false},
 	}

+ 2 - 2
scanner/mailscanner.go

@@ -167,7 +167,7 @@ func (ms *MailScanner) readEmailMaps() {
 
 		mails := ms.readMailFile(mailPath)
 		for _, mail := range mails {
-			ms.storage.SaveMail(mailbox, common.Inbox, mail)
+			ms.storage.SaveMail(mailbox, common.Inbox, mail, false)
 		}
 		log.Printf("New email for %s, emails read %d", mailPath, len(mails))
 
@@ -209,7 +209,7 @@ func (ms *MailScanner) Run() {
 					if mailbox != "" {
 						mails := ms.readMailFile(mailPath)
 						for _, mail := range mails {
-							ms.storage.SaveMail(mailbox, common.Inbox, mail)
+							ms.storage.SaveMail(mailbox, common.Inbox, mail, false)
 						}
 						log.Printf("New email for %s, emails read %d", mailPath, len(mails))
 					} else {

+ 24 - 7
web/mailbox.go

@@ -31,6 +31,7 @@ import (
 	"encoding/hex"
 	"encoding/json"
 	"fmt"
+	"html"
 	template "html/template"
 	"log"
 	"net/http"
@@ -229,7 +230,21 @@ func (s *Server) extractFolder(email string, r *http.Request) string {
 }
 
 func (s *Server) handleNewMail(w http.ResponseWriter, r *http.Request, user, email string) {
-	to := r.FormValue("to")
+
+	rawMail := common.Mail{
+		Header: &common.MailHeader{
+			From:    email,
+			To:      r.FormValue("to"),
+			Cc:      r.FormValue("cc"),
+			Bcc:     r.FormValue("bcc"),
+			Date:    time.Now().Unix(),
+			Subject: r.FormValue("subject"),
+		},
+		Body: &common.MailBody{
+			PlainText: html.EscapeString(r.FormValue("body")),
+		},
+	}
+
 	resultEmail := s.templater.ExecuteMail(&struct {
 		From    string
 		Subject string
@@ -237,11 +252,11 @@ func (s *Server) handleNewMail(w http.ResponseWriter, r *http.Request, user, ema
 		To      string
 		Body    template.HTML
 	}{
-		From:    email,
-		To:      to,
-		Subject: r.FormValue("subject"),
-		Date:    template.HTML(time.Now().Format(time.RFC1123Z)),
-		Body:    template.HTML(r.FormValue("body")),
+		From:    rawMail.Header.From,
+		To:      rawMail.Header.To,
+		Subject: rawMail.Header.Subject,
+		Date:    template.HTML(time.Unix(rawMail.Header.Date, 0).Format(time.RFC1123Z)),
+		Body:    template.HTML(rawMail.Body.PlainText),
 	})
 
 	host := config.ConfigInstance().MyDomain
@@ -282,7 +297,7 @@ func (s *Server) handleNewMail(w http.ResponseWriter, r *http.Request, user, ema
 		return
 	}
 
-	err = client.Rcpt(to)
+	err = client.Rcpt(rawMail.Header.To)
 	if err != nil {
 		s.error(http.StatusInternalServerError, "Unable to send message", w)
 		log.Println(err)
@@ -311,6 +326,8 @@ func (s *Server) handleNewMail(w http.ResponseWriter, r *http.Request, user, ema
 	}
 
 	client.Quit()
+
+	s.storage.SaveMail(email, common.Sent, &rawMail, true)
 	w.WriteHeader(http.StatusOK)
 	w.Write([]byte{0})
 }