From 36923a0dc23567e58e9c38f6e7ba96b8bf41db68 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 3 Dec 2019 16:46:03 +0100 Subject: Pre-fill composer with sender address --- public/compose.html | 8 ++++---- server.go | 20 +++++++++++++------- smtp.go | 24 +++++++++++++----------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/public/compose.html b/public/compose.html index 8253eeb..933083c 100644 --- a/public/compose.html +++ b/public/compose.html @@ -10,13 +10,13 @@

From:

- +

To:

- +

Subject:

- +

Body:

- +

diff --git a/server.go b/server.go index 6a12122..c4eb6d7 100644 --- a/server.go +++ b/server.go @@ -202,6 +202,11 @@ func handleGetPart(ctx *context, raw bool) error { func handleCompose(ectx echo.Context) error { ctx := ectx.(*context) + var msg OutgoingMessage + if strings.ContainsRune(ctx.session.username, '@') { + msg.From = ctx.session.username + } + if ctx.Request().Method == http.MethodPost { // TODO: parse address lists from := ctx.FormValue("from") @@ -220,12 +225,11 @@ func handleCompose(ectx echo.Context) error { return echo.NewHTTPError(http.StatusForbidden, err) } - msg := OutgoingMessage{ - from: from, - to: []string{to}, - subject: subject, - text: text, - } + msg.From = from + msg.To = []string{to} + msg.Subject = subject + msg.Text = text + if err := sendMessage(c, &msg); err != nil { return err } @@ -239,7 +243,9 @@ func handleCompose(ectx echo.Context) error { return ctx.Redirect(http.StatusFound, "/mailbox/INBOX") } - return ctx.Render(http.StatusOK, "compose.html", nil) + return ctx.Render(http.StatusOK, "compose.html", map[string]interface{}{ + "Message": &msg, + }) } func New(imapURL, smtpURL string) *echo.Echo { diff --git a/smtp.go b/smtp.go index d4b8ea2..d1a37da 100644 --- a/smtp.go +++ b/smtp.go @@ -34,17 +34,17 @@ func (s *Server) connectSMTP() (*smtp.Client, error) { } type OutgoingMessage struct { - from string - to []string - subject string - text string + From string + To []string + Subject string + Text string } func (msg *OutgoingMessage) WriteTo(w io.Writer) error { - from := []*mail.Address{{"", msg.from}} + from := []*mail.Address{{"", msg.From}} - to := make([]*mail.Address, len(msg.to)) - for i, addr := range msg.to { + to := make([]*mail.Address, len(msg.To)) + for i, addr := range msg.To { to[i] = &mail.Address{"", addr} } @@ -52,7 +52,9 @@ func (msg *OutgoingMessage) WriteTo(w io.Writer) error { h.SetDate(time.Now()) h.SetAddressList("From", from) h.SetAddressList("To", to) - h.SetText("Subject", msg.subject) + if msg.Subject != "" { + h.SetText("Subject", msg.Subject) + } mw, err := mail.CreateWriter(w, h) if err != nil { @@ -68,7 +70,7 @@ func (msg *OutgoingMessage) WriteTo(w io.Writer) error { } defer tw.Close() - if _, err := io.WriteString(tw, msg.text); err != nil { + if _, err := io.WriteString(tw, msg.Text); err != nil { return fmt.Errorf("failed to write text part: %v", err) } @@ -84,11 +86,11 @@ func (msg *OutgoingMessage) WriteTo(w io.Writer) error { } func sendMessage(c *smtp.Client, msg *OutgoingMessage) error { - if err := c.Mail(msg.from, nil); err != nil { + if err := c.Mail(msg.From, nil); err != nil { return fmt.Errorf("MAIL FROM failed: %v", err) } - for _, to := range msg.to { + for _, to := range msg.To { if err := c.Rcpt(to); err != nil { return fmt.Errorf("RCPT TO failed: %v", err) } -- cgit v1.2.3