Pre-fill composer with sender address

This commit is contained in:
Simon Ser 2019-12-03 16:46:03 +01:00
parent 89cebfd819
commit 36923a0dc2
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
3 changed files with 30 additions and 22 deletions

View file

@ -10,13 +10,13 @@
<form method="post" action="/compose"> <form method="post" action="/compose">
<p>From:</p> <p>From:</p>
<input type="text" name="from"> <input type="email" name="from" value="{{.Message.From}}">
<p>To:</p> <p>To:</p>
<input type="text" name="to"> <input type="email" name="to" multiple>
<p>Subject:</p> <p>Subject:</p>
<input type="text" name="subject"> <input type="text" name="subject" value="{{.Message.Subject}}">
<p>Body:</p> <p>Body:</p>
<textarea name="text" cols="80" rows="20"></textarea> <textarea name="text" cols="80" rows="20">{{.Message.Text}}</textarea>
<br><br> <br><br>
<input type="submit" value="Send"> <input type="submit" value="Send">
</form> </form>

View file

@ -202,6 +202,11 @@ func handleGetPart(ctx *context, raw bool) error {
func handleCompose(ectx echo.Context) error { func handleCompose(ectx echo.Context) error {
ctx := ectx.(*context) ctx := ectx.(*context)
var msg OutgoingMessage
if strings.ContainsRune(ctx.session.username, '@') {
msg.From = ctx.session.username
}
if ctx.Request().Method == http.MethodPost { if ctx.Request().Method == http.MethodPost {
// TODO: parse address lists // TODO: parse address lists
from := ctx.FormValue("from") from := ctx.FormValue("from")
@ -220,12 +225,11 @@ func handleCompose(ectx echo.Context) error {
return echo.NewHTTPError(http.StatusForbidden, err) return echo.NewHTTPError(http.StatusForbidden, err)
} }
msg := OutgoingMessage{ msg.From = from
from: from, msg.To = []string{to}
to: []string{to}, msg.Subject = subject
subject: subject, msg.Text = text
text: text,
}
if err := sendMessage(c, &msg); err != nil { if err := sendMessage(c, &msg); err != nil {
return err return err
} }
@ -239,7 +243,9 @@ func handleCompose(ectx echo.Context) error {
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX") 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 { func New(imapURL, smtpURL string) *echo.Echo {

24
smtp.go
View file

@ -34,17 +34,17 @@ func (s *Server) connectSMTP() (*smtp.Client, error) {
} }
type OutgoingMessage struct { type OutgoingMessage struct {
from string From string
to []string To []string
subject string Subject string
text string Text string
} }
func (msg *OutgoingMessage) WriteTo(w io.Writer) error { 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)) to := make([]*mail.Address, len(msg.To))
for i, addr := range msg.to { for i, addr := range msg.To {
to[i] = &mail.Address{"", addr} to[i] = &mail.Address{"", addr}
} }
@ -52,7 +52,9 @@ func (msg *OutgoingMessage) WriteTo(w io.Writer) error {
h.SetDate(time.Now()) h.SetDate(time.Now())
h.SetAddressList("From", from) h.SetAddressList("From", from)
h.SetAddressList("To", to) h.SetAddressList("To", to)
h.SetText("Subject", msg.subject) if msg.Subject != "" {
h.SetText("Subject", msg.Subject)
}
mw, err := mail.CreateWriter(w, h) mw, err := mail.CreateWriter(w, h)
if err != nil { if err != nil {
@ -68,7 +70,7 @@ func (msg *OutgoingMessage) WriteTo(w io.Writer) error {
} }
defer tw.Close() 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) 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 { 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) 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 { if err := c.Rcpt(to); err != nil {
return fmt.Errorf("RCPT TO failed: %v", err) return fmt.Errorf("RCPT TO failed: %v", err)
} }