summaryrefslogtreecommitdiff
path: root/srv/mailinglist/mailer.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2021-08-07 20:38:37 -0600
committerBrian Picciano <mediocregopher@gmail.com>2021-08-07 20:38:37 -0600
commit0197d9cd493b5785bca05f476856540ec64da64a (patch)
treedb19ac4bfa602b1e0b001769c57d6b7c37d96fc4 /srv/mailinglist/mailer.go
parentdce39b836a0fd6e37ab2499c2e0e232572c17ad6 (diff)
split configuration parsing out into separate packages, split api out as well
Diffstat (limited to 'srv/mailinglist/mailer.go')
-rw-r--r--srv/mailinglist/mailer.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/srv/mailinglist/mailer.go b/srv/mailinglist/mailer.go
index 12fc398..b65ccb8 100644
--- a/srv/mailinglist/mailer.go
+++ b/srv/mailinglist/mailer.go
@@ -1,8 +1,14 @@
package mailinglist
import (
+ "context"
+ "errors"
+ "strings"
+
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
+ "github.com/mediocregopher/blog.mediocregopher.com/srv/cfg"
+ "github.com/mediocregopher/mediocre-go-lib/v2/mctx"
)
// Mailer is used to deliver emails to arbitrary recipients.
@@ -30,6 +36,39 @@ type MailerParams struct {
SendAs string
}
+// SetupCfg implement the cfg.Cfger interface.
+func (m *MailerParams) SetupCfg(cfg *cfg.Cfg) {
+
+ cfg.StringVar(&m.SMTPAddr, "ml-smtp-addr", "", "Address of SMTP server to use for sending emails for the mailing list")
+ smtpAuthStr := cfg.String("ml-smtp-auth", "", "user:pass to use when authenticating with the mailing list SMTP server. The given user will also be used as the From address.")
+
+ cfg.OnInit(func(ctx context.Context) error {
+ if m.SMTPAddr == "" {
+ return nil
+ }
+
+ smtpAuthParts := strings.SplitN(*smtpAuthStr, ":", 2)
+ if len(smtpAuthParts) < 2 {
+ return errors.New("invalid -ml-smtp-auth")
+ }
+
+ m.SMTPAuth = sasl.NewPlainClient("", smtpAuthParts[0], smtpAuthParts[1])
+ m.SendAs = smtpAuthParts[0]
+
+ return nil
+ })
+}
+
+// Annotate implements mctx.Annotator interface.
+func (m *MailerParams) Annotate(a mctx.Annotations) {
+ if m.SMTPAddr == "" {
+ return
+ }
+
+ a["smtpAddr"] = m.SMTPAddr
+ a["smtpSendAs"] = m.SendAs
+}
+
type mailer struct {
params MailerParams
}