[PATCH] Add optional Cc and Bcc fields when composing a message
Export this patch
This should address https://todo.sr.ht/~migadu/alps/145.
---
plugins/base/routes.go | 3 ++-
plugins/base/smtp.go | 35 ++++++++++++++++++++++++++---------
plugins/base/strconv.go | 12 +++++++++---
themes/alps/compose.html | 14 ++++++++++++++
4 files changed, 51 insertions(+), 13 deletions(-)
diff --git a/plugins/base/routes.go b/plugins/base/routes.go
index 482b368..ba3de30 100644
--- a/plugins/base/routes.go
+++ b/plugins/base/routes.go
@@ -595,6 +595,8 @@ func handleCompose(ctx *alps.Context, msg *OutgoingMessage, options *composeOpti
msg.From = ctx.FormValue("from")
msg.To = parseAddressList(ctx.FormValue("to"))
+ msg.Cc = parseAddressList(ctx.FormValue("cc"))
+ msg.Bcc = parseAddressList(ctx.FormValue("bcc"))
msg.Subject = ctx.FormValue("subject")
msg.Text = ctx.FormValue("text")
msg.InReplyTo = ctx.FormValue("in_reply_to")
@@ -735,7 +737,6 @@ func handleComposeNew(ctx *alps.Context) error {
}
// These are common mailto URL query parameters
- // TODO: cc, bcc
var hdr mail.Header
hdr.GenerateMessageID()
mid, _ := hdr.MessageID()
diff --git a/plugins/base/smtp.go b/plugins/base/smtp.go
index bf91c24..be9aedc 100644
--- a/plugins/base/smtp.go
+++ b/plugins/base/smtp.go
@@ -81,6 +81,8 @@ func (att *imapAttachment) Filename() string {
type OutgoingMessage struct {
From string
To []string
+ Cc []string
+ Bcc []string
Subject string
MessageID string
InReplyTo string
@@ -123,6 +125,19 @@ func writeAttachment(mw *mail.Writer, att Attachment) error {
return nil
}
+func prepareAddressList(addresses []string) ([]*mail.Address, error) {
+ l := make([]*mail.Address, len(addresses))
+ for i, rcpt := range addresses {
+ addr, err := mail.ParseAddress(rcpt)
+ if err != nil {
+ return nil, err
+ }
+ l[i] = addr
+ }
+
+ return l, nil
+}
+
func (msg *OutgoingMessage) WriteTo(w io.Writer) error {
fromAddr, err := mail.ParseAddress(msg.From)
if err != nil {
@@ -130,19 +145,21 @@ func (msg *OutgoingMessage) WriteTo(w io.Writer) error {
}
from := []*mail.Address{fromAddr}
- to := make([]*mail.Address, len(msg.To))
- for i, rcpt := range msg.To {
- addr, err := mail.ParseAddress(rcpt)
- if err != nil {
- return err
- }
- to[i] = addr
+ to, err := prepareAddressList(msg.To)
+ if err != nil {
+ return err
+ }
+
+ cc, err := prepareAddressList(msg.Cc)
+ if err != nil {
+ return err
}
var h mail.Header
h.SetDate(time.Now())
h.SetAddressList("From", from)
h.SetAddressList("To", to)
+ h.SetAddressList("Cc", cc)
if msg.Subject != "" {
h.SetText("Subject", msg.Subject)
}
@@ -200,10 +217,10 @@ func sendMessage(c *smtp.Client, msg *OutgoingMessage) error {
return fmt.Errorf("MAIL FROM failed: %v", err)
}
- for _, to := range msg.To {
+ for _, to := range append(msg.To, append(msg.Bcc, msg.Cc...)...) {
addr, err := mail.ParseAddress(to)
if err != nil {
- return fmt.Errorf("parsing 'To' address failed: %v", err)
+ return fmt.Errorf("parsing address %q failed: %v", to, err)
}
if err := c.Rcpt(addr.Address, nil); err != nil {
diff --git a/plugins/base/strconv.go b/plugins/base/strconv.go
index 4266e00..4868d71 100644
--- a/plugins/base/strconv.go
+++ b/plugins/base/strconv.go
@@ -64,8 +64,14 @@ func parsePartPath(s string) ([]int, error) {
func parseAddressList(s string) []string {
l := strings.Split(s, ",")
- for i, addr := range l {
- l[i] = strings.TrimSpace(addr)
+ ret := make([]string, 0, len(l))
+ for _, addr := range l {
+ if addr == "" {
+ continue
+ }
+
+ ret = append(ret, strings.TrimSpace(addr))
}
- return l
+
+ return ret
}
diff --git a/themes/alps/compose.html b/themes/alps/compose.html
index 75345b2..7d7c822 100644
--- a/themes/alps/compose.html
+++ b/themes/alps/compose.html
@@ -29,6 +29,20 @@
{{ if not $to }} autofocus{{ end }}
/>
+ <label>Cc</label>
+ <input
+ type="text"
+ name="cc"
+ id="cc"
+ />
+
+ <label>Bcc</label>
+ <input
+ type="text"
+ name="bcc"
+ id="bcc"
+ />
+
<label>Subject</label>
<input
type="text"
--
2.43.0
Pushed, thanks!