add plaintext email option, use text/template

text/template is used on plaintext emails to avoid escaping of certain
characters.
pull/61/head
Harvey Tindall 4 years ago
parent 27ef931670
commit 93b5b483cc
No known key found for this signature in database
GPG Key ID: BBC65952848FB1A2

@ -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."
}
}
},

@ -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), "</p>"), "<p>")
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

@ -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

@ -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=

@ -1,3 +1,3 @@
{{ .text }}
{{ .plaintext }}
{{ .message }}

Loading…
Cancel
Save