From 93b5b483cc7e496af96fc2233635fa6843c375df Mon Sep 17 00:00:00 2001
From: Harvey Tindall
Date: Thu, 18 Feb 2021 18:26:23 +0000
Subject: [PATCH] add plaintext email option, use text/template
text/template is used on plaintext emails to avoid escaping of certain
characters.
---
config/config-base.json | 11 ++++++++++-
email.go | 30 +++++++++++++++++++++++++-----
go.mod | 1 +
go.sum | 2 ++
mail/announcement.txt | 2 +-
5 files changed, 39 insertions(+), 7 deletions(-)
diff --git a/config/config-base.json b/config/config-base.json
index 1d6a6d3..9119fc1 100644
--- a/config/config-base.json
+++ b/config/config-base.json
@@ -73,7 +73,7 @@
"requires_restart": true,
"type": "text",
"value": "",
- "description": "Optionally substitute occurrences of \"Jellyfin\" in the account creation form with this. May result in bad grammar."
+ "description": "Optionally substitute occurrences of \"Jellyfin\" in the account creation form and emails with this. May result in bad grammar."
}
}
},
@@ -398,6 +398,15 @@
"type": "text",
"value": "Jellyfin",
"description": "The name of the sender"
+ },
+ "plaintext": {
+ "name": "Send emails as plain text",
+ "required": false,
+ "requires_restart": false,
+ "depends_true": "method",
+ "type": "bool",
+ "value": false,
+ "description": "Send emails as plain text instead of HTML."
}
}
},
diff --git a/email.go b/email.go
index f0fe3d2..97b91e5 100644
--- a/email.go
+++ b/email.go
@@ -6,9 +6,11 @@ import (
"crypto/tls"
"fmt"
"html/template"
+ "io"
"net/smtp"
"strings"
"sync"
+ textTemplate "text/template"
"time"
"github.com/gomarkdown/markdown"
@@ -16,6 +18,7 @@ import (
jEmail "github.com/jordan-wright/email"
"github.com/knz/strtime"
"github.com/mailgun/mailgun-go/v4"
+ stripmd "github.com/writeas/go-strip-markdown"
)
// implements email sending, right now via smtp or mailgun.
@@ -167,16 +170,31 @@ func (emailer *Emailer) NewSMTP(server string, port int, username, password stri
}
}
+type templ interface {
+ Execute(wr io.Writer, data interface{}) error
+}
+
func (emailer *Emailer) construct(app *appContext, section, keyFragment string, data map[string]interface{}) (html, text string, err error) {
- var tpl *template.Template
+ var tpl templ
if substituteStrings == "" {
data["jellyfin"] = "Jellyfin"
} else {
data["jellyfin"] = substituteStrings
}
- for _, key := range []string{"html", "text"} {
+ var keys []string
+ if app.config.Section("email").Key("plaintext").MustBool(false) {
+ keys = []string{"text"}
+ text = ""
+ } else {
+ keys = []string{"html", "text"}
+ }
+ for _, key := range keys {
filesystem, fpath := app.GetPath(section, keyFragment+key)
- tpl, err = template.ParseFS(filesystem, fpath)
+ if key == "html" {
+ tpl, err = template.ParseFS(filesystem, fpath)
+ } else {
+ tpl, err = textTemplate.ParseFS(filesystem, fpath)
+ }
if err != nil {
return
}
@@ -220,11 +238,13 @@ func (emailer *Emailer) constructAnnouncement(subject, md string, app *appContex
email := &Email{subject: subject}
renderer := html.NewRenderer(html.RendererOptions{Flags: html.Smartypants})
html := markdown.ToHTML([]byte(md), nil, renderer)
+ text := strings.TrimPrefix(strings.TrimSuffix(stripmd.Strip(md), "
"), "")
message := app.config.Section("email").Key("message").String()
var err error
email.html, email.text, err = emailer.construct(app, "announcement_email", "email_", map[string]interface{}{
- "text": template.HTML(html),
- "message": message,
+ "text": template.HTML(html),
+ "plaintext": text,
+ "message": message,
})
if err != nil {
return nil, err
diff --git a/go.mod b/go.mod
index 8f4c777..6371f51 100644
--- a/go.mod
+++ b/go.mod
@@ -38,6 +38,7 @@ require (
github.com/swaggo/gin-swagger v1.3.0
github.com/swaggo/swag v1.7.0 // indirect
github.com/ugorji/go v1.2.0 // indirect
+ github.com/writeas/go-strip-markdown v2.0.1+incompatible // indirect
golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/tools v0.1.0 // indirect
diff --git a/go.sum b/go.sum
index 8b84f9c..bd05275 100644
--- a/go.sum
+++ b/go.sum
@@ -226,6 +226,8 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
+github.com/writeas/go-strip-markdown v2.0.1+incompatible h1:IIqxTM5Jr7RzhigcL6FkrCNfXkvbR+Nbu1ls48pXYcw=
+github.com/writeas/go-strip-markdown v2.0.1+incompatible/go.mod h1:Rsyu10ZhbEK9pXdk8V6MVnZmTzRG0alMNLMwa0J01fE=
github.com/yuin/goldmark v1.2.1 h1:ruQGxdhGHe7FWOJPT0mKs5+pD2Xs1Bm/kdGlHO04FmM=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/dl v0.0.0-20190829154251-82a15e2f2ead h1:jeP6FgaSLNTMP+Yri3qjlACywQLye+huGLmNGhBzm6k=
diff --git a/mail/announcement.txt b/mail/announcement.txt
index 83045a4..d6af7d2 100644
--- a/mail/announcement.txt
+++ b/mail/announcement.txt
@@ -1,3 +1,3 @@
-{{ .text }}
+{{ .plaintext }}
{{ .message }}