From 1aa32bf37742770a3243460ca4445256fe4273a7 Mon Sep 17 00:00:00 2001 From: Evan Gates Date: Tue, 5 Apr 2022 08:54:58 -0600 Subject: compose: parse headers correctly from -H By using :compose -H
a user should be able to add arbitrary headers to an email. The existing implementation from 5b523880b4b4cd2abd9457b4b09c384af33be14b added the headers as lines to the beginning of the body. These lines were not interpreted as headers anywhere and ended up as plain text in the body of the email. Fix the code to parse and add the headers correctly. Signed-off-by: Evan Gates Tested-by: Moritz Poldrack --- commands/account/compose.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'commands/account/compose.go') diff --git a/commands/account/compose.go b/commands/account/compose.go index 4884b113..fc7617ec 100644 --- a/commands/account/compose.go +++ b/commands/account/compose.go @@ -2,9 +2,14 @@ package account import ( "errors" + "fmt" + "io" + gomail "net/mail" "regexp" "strings" + "github.com/emersion/go-message/mail" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/widgets" @@ -38,9 +43,17 @@ func (Compose) Execute(aerc *widgets.Aerc, args []string) error { template = aerc.Config().Templates.NewMessage } + msg, err := gomail.ReadMessage(strings.NewReader(body)) + if errors.Is(err, io.EOF) { // completely empty + msg = &gomail.Message{Body: strings.NewReader("")} + } else if err != nil { + return fmt.Errorf("mail.ReadMessage: %w", err) + } + headers := mail.HeaderFromMap(msg.Header) + composer, err := widgets.NewComposer(aerc, acct, aerc.Config(), acct.AccountConfig(), acct.Worker(), - template, nil, models.OriginalMail{}) + template, &headers, models.OriginalMail{}) if err != nil { return err } @@ -56,7 +69,7 @@ func (Compose) Execute(aerc *widgets.Aerc, args []string) error { go func() { defer logging.PanicHandler() - composer.AppendContents(strings.NewReader(body)) + composer.AppendContents(msg.Body) }() return nil } -- cgit