2020-07-31 11:48:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-08-02 16:20:50 +00:00
|
|
|
"crypto/tls"
|
2021-03-15 21:57:42 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
2020-07-31 11:48:37 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2021-02-18 18:26:23 +00:00
|
|
|
"io"
|
2021-05-07 15:33:44 +00:00
|
|
|
"io/fs"
|
2020-07-31 11:48:37 +00:00
|
|
|
"net/smtp"
|
2021-03-15 21:57:42 +00:00
|
|
|
"os"
|
2021-04-02 14:56:34 +00:00
|
|
|
"strconv"
|
2020-07-31 11:48:37 +00:00
|
|
|
"strings"
|
2021-02-18 14:58:53 +00:00
|
|
|
"sync"
|
2021-02-18 18:26:23 +00:00
|
|
|
textTemplate "text/template"
|
2020-07-31 11:48:37 +00:00
|
|
|
"time"
|
2020-08-16 12:36:54 +00:00
|
|
|
|
2021-02-18 14:58:53 +00:00
|
|
|
"github.com/gomarkdown/markdown"
|
|
|
|
"github.com/gomarkdown/markdown/html"
|
2021-04-06 20:25:44 +00:00
|
|
|
"github.com/itchyny/timefmt-go"
|
2020-08-16 12:36:54 +00:00
|
|
|
jEmail "github.com/jordan-wright/email"
|
|
|
|
"github.com/mailgun/mailgun-go/v4"
|
2020-07-31 11:48:37 +00:00
|
|
|
)
|
|
|
|
|
2020-09-13 20:07:15 +00:00
|
|
|
// implements email sending, right now via smtp or mailgun.
|
2021-05-07 00:08:12 +00:00
|
|
|
type EmailClient interface {
|
|
|
|
Send(fromName, fromAddr string, message *Message, address ...string) error
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
type DummyClient struct{}
|
2021-04-08 13:20:13 +00:00
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (dc *DummyClient) Send(fromName, fromAddr string, email *Message, address ...string) error {
|
2021-04-08 13:20:13 +00:00
|
|
|
fmt.Printf("FROM: %s <%s>\nTO: %s\nTEXT: %s\n", fromName, fromAddr, strings.Join(address, ", "), email.Text)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// Mailgun client implements emailClient.
|
2020-09-13 20:07:15 +00:00
|
|
|
type Mailgun struct {
|
|
|
|
client *mailgun.MailgunImpl
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (mg *Mailgun) Send(fromName, fromAddr string, email *Message, address ...string) error {
|
2020-09-13 20:07:15 +00:00
|
|
|
message := mg.client.NewMessage(
|
|
|
|
fmt.Sprintf("%s <%s>", fromName, fromAddr),
|
2021-02-20 22:49:59 +00:00
|
|
|
email.Subject,
|
|
|
|
email.Text,
|
2020-09-13 20:07:15 +00:00
|
|
|
)
|
2021-02-18 14:58:53 +00:00
|
|
|
for _, a := range address {
|
|
|
|
// Adding variable tells mailgun to do a batch send, so users don't see other recipients.
|
|
|
|
message.AddRecipientAndVariables(a, map[string]interface{}{"unique_id": a})
|
|
|
|
}
|
2021-02-20 22:49:59 +00:00
|
|
|
message.SetHtml(email.HTML)
|
2020-09-13 20:07:15 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
_, _, err := mg.client.Send(ctx, message)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// SMTP supports SSL/TLS and STARTTLS; implements emailClient.
|
|
|
|
type SMTP struct {
|
2021-03-15 21:57:42 +00:00
|
|
|
sslTLS bool
|
|
|
|
server string
|
|
|
|
port int
|
|
|
|
auth smtp.Auth
|
|
|
|
tlsConfig *tls.Config
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (sm *SMTP) Send(fromName, fromAddr string, email *Message, address ...string) error {
|
2020-11-14 15:33:25 +00:00
|
|
|
server := fmt.Sprintf("%s:%d", sm.server, sm.port)
|
2021-02-18 14:58:53 +00:00
|
|
|
from := fmt.Sprintf("%s <%s>", fromName, fromAddr)
|
|
|
|
var wg sync.WaitGroup
|
2020-09-13 20:07:15 +00:00
|
|
|
var err error
|
2021-02-18 14:58:53 +00:00
|
|
|
for _, addr := range address {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(addr string) {
|
|
|
|
defer wg.Done()
|
|
|
|
e := jEmail.NewEmail()
|
2021-02-20 22:49:59 +00:00
|
|
|
e.Subject = email.Subject
|
2021-02-18 14:58:53 +00:00
|
|
|
e.From = from
|
2021-02-20 22:49:59 +00:00
|
|
|
e.Text = []byte(email.Text)
|
|
|
|
e.HTML = []byte(email.HTML)
|
2021-02-18 14:58:53 +00:00
|
|
|
e.To = []string{addr}
|
|
|
|
if sm.sslTLS {
|
2021-03-15 21:57:42 +00:00
|
|
|
err = e.SendWithTLS(server, sm.auth, sm.tlsConfig)
|
2021-02-18 14:58:53 +00:00
|
|
|
} else {
|
2021-03-15 21:57:42 +00:00
|
|
|
err = e.SendWithStartTLS(server, sm.auth, sm.tlsConfig)
|
2021-02-18 14:58:53 +00:00
|
|
|
}
|
|
|
|
}(addr)
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
2021-02-18 14:58:53 +00:00
|
|
|
wg.Wait()
|
2020-09-13 20:07:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
// Emailer contains the email sender, translations, and methods to construct messages.
|
2020-07-31 11:48:37 +00:00
|
|
|
type Emailer struct {
|
2020-09-13 20:07:15 +00:00
|
|
|
fromAddr, fromName string
|
2021-01-19 00:29:29 +00:00
|
|
|
lang emailLang
|
2021-05-07 00:08:12 +00:00
|
|
|
sender EmailClient
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
// Message stores content.
|
|
|
|
type Message struct {
|
2021-05-07 15:33:44 +00:00
|
|
|
Subject string `json:"subject"`
|
|
|
|
HTML string `json:"html"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Markdown string `json:"markdown"`
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
func (emailer *Emailer) formatExpiry(expiry time.Time, tzaware bool, datePattern, timePattern string) (d, t, expiresIn string) {
|
2021-04-06 20:25:44 +00:00
|
|
|
d = timefmt.Format(expiry, datePattern)
|
|
|
|
t = timefmt.Format(expiry, timePattern)
|
2020-09-15 11:00:20 +00:00
|
|
|
currentTime := time.Now()
|
2020-07-31 11:48:37 +00:00
|
|
|
if tzaware {
|
2020-09-15 11:00:20 +00:00
|
|
|
currentTime = currentTime.UTC()
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-15 11:00:20 +00:00
|
|
|
_, _, days, hours, minutes, _ := timeDiff(expiry, currentTime)
|
2020-07-31 11:48:37 +00:00
|
|
|
if days != 0 {
|
2021-04-02 14:56:34 +00:00
|
|
|
expiresIn += strconv.Itoa(days) + "d "
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if hours != 0 {
|
2021-04-02 14:56:34 +00:00
|
|
|
expiresIn += strconv.Itoa(hours) + "h "
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if minutes != 0 {
|
2021-04-02 14:56:34 +00:00
|
|
|
expiresIn += strconv.Itoa(minutes) + "m "
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-15 11:00:20 +00:00
|
|
|
expiresIn = strings.TrimSuffix(expiresIn, " ")
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// NewEmailer configures and returns a new emailer.
|
2020-09-13 20:07:15 +00:00
|
|
|
func NewEmailer(app *appContext) *Emailer {
|
|
|
|
emailer := &Emailer{
|
|
|
|
fromAddr: app.config.Section("email").Key("address").String(),
|
|
|
|
fromName: app.config.Section("email").Key("from").String(),
|
2021-01-19 00:29:29 +00:00
|
|
|
lang: app.storage.lang.Email[app.storage.lang.chosenEmailLang],
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
method := app.config.Section("email").Key("method").String()
|
|
|
|
if method == "smtp" {
|
|
|
|
sslTls := false
|
|
|
|
if app.config.Section("smtp").Key("encryption").String() == "ssl_tls" {
|
|
|
|
sslTls = true
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-10-11 21:32:51 +00:00
|
|
|
username := ""
|
|
|
|
if u := app.config.Section("smtp").Key("username").MustString(""); u != "" {
|
|
|
|
username = u
|
|
|
|
} else {
|
|
|
|
username = emailer.fromAddr
|
|
|
|
}
|
2021-03-15 21:57:42 +00:00
|
|
|
err := emailer.NewSMTP(app.config.Section("smtp").Key("server").String(), app.config.Section("smtp").Key("port").MustInt(465), username, app.config.Section("smtp").Key("password").String(), sslTls, app.config.Section("smtp").Key("ssl_cert").MustString(""))
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("Error while initiating SMTP mailer: %v", err)
|
|
|
|
}
|
2020-09-13 20:07:15 +00:00
|
|
|
} else if method == "mailgun" {
|
|
|
|
emailer.NewMailgun(app.config.Section("mailgun").Key("api_url").String(), app.config.Section("mailgun").Key("api_key").String())
|
2021-04-08 13:20:13 +00:00
|
|
|
} else if method == "dummy" {
|
2021-05-07 00:08:12 +00:00
|
|
|
emailer.sender = &DummyClient{}
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
return emailer
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// NewMailgun returns a Mailgun emailClient.
|
2020-09-13 20:07:15 +00:00
|
|
|
func (emailer *Emailer) NewMailgun(url, key string) {
|
|
|
|
sender := &Mailgun{
|
|
|
|
client: mailgun.NewMailgun(strings.Split(emailer.fromAddr, "@")[1], key),
|
|
|
|
}
|
2021-01-19 00:29:29 +00:00
|
|
|
// Mailgun client takes the base url, so we need to trim off the end (e.g 'v3/messages')
|
2020-09-13 20:07:15 +00:00
|
|
|
if strings.Contains(url, "messages") {
|
|
|
|
url = url[0:strings.LastIndex(url, "/")]
|
|
|
|
url = url[0:strings.LastIndex(url, "/")]
|
|
|
|
}
|
|
|
|
sender.client.SetAPIBase(url)
|
|
|
|
emailer.sender = sender
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// NewSMTP returns an SMTP emailClient.
|
2021-03-15 21:57:42 +00:00
|
|
|
func (emailer *Emailer) NewSMTP(server string, port int, username, password string, sslTLS bool, certPath string) (err error) {
|
2021-05-17 00:16:59 +00:00
|
|
|
// x509.SystemCertPool is unavailable on windows
|
|
|
|
if PLATFORM == "windows" {
|
|
|
|
emailer.sender = &SMTP{
|
|
|
|
auth: smtp.PlainAuth("", username, password, server),
|
|
|
|
server: server,
|
|
|
|
port: port,
|
|
|
|
sslTLS: sslTLS,
|
|
|
|
tlsConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: false,
|
|
|
|
ServerName: server,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-03-15 21:57:42 +00:00
|
|
|
rootCAs, err := x509.SystemCertPool()
|
|
|
|
if rootCAs == nil || err != nil {
|
|
|
|
rootCAs = x509.NewCertPool()
|
|
|
|
}
|
|
|
|
if certPath != "" {
|
|
|
|
var cert []byte
|
|
|
|
cert, err = os.ReadFile(certPath)
|
|
|
|
if rootCAs.AppendCertsFromPEM(cert) == false {
|
|
|
|
err = errors.New("Failed to append cert to pool")
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 11:00:20 +00:00
|
|
|
emailer.sender = &SMTP{
|
2020-11-14 15:33:25 +00:00
|
|
|
auth: smtp.PlainAuth("", username, password, server),
|
2020-09-13 20:07:15 +00:00
|
|
|
server: server,
|
|
|
|
port: port,
|
2020-09-15 11:00:20 +00:00
|
|
|
sslTLS: sslTLS,
|
2021-03-15 21:57:42 +00:00
|
|
|
tlsConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: false,
|
|
|
|
ServerName: server,
|
|
|
|
RootCAs: rootCAs,
|
|
|
|
},
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2021-03-15 21:57:42 +00:00
|
|
|
return
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 18:26:23 +00:00
|
|
|
type templ interface {
|
|
|
|
Execute(wr io.Writer, data interface{}) error
|
|
|
|
}
|
|
|
|
|
2021-05-07 15:33:44 +00:00
|
|
|
func (emailer *Emailer) construct(app *appContext, section, keyFragment string, data map[string]interface{}) (html, text, markdown string, err error) {
|
2021-02-18 18:26:23 +00:00
|
|
|
var tpl templ
|
2021-02-18 17:47:15 +00:00
|
|
|
if substituteStrings == "" {
|
|
|
|
data["jellyfin"] = "Jellyfin"
|
|
|
|
} else {
|
|
|
|
data["jellyfin"] = substituteStrings
|
|
|
|
}
|
2021-02-18 18:26:23 +00:00
|
|
|
var keys []string
|
2021-05-07 15:33:44 +00:00
|
|
|
plaintext := app.config.Section("email").Key("plaintext").MustBool(false)
|
|
|
|
if plaintext {
|
2021-05-21 20:35:25 +00:00
|
|
|
if telegramEnabled || discordEnabled {
|
2021-05-07 15:33:44 +00:00
|
|
|
keys = []string{"text"}
|
|
|
|
text, markdown = "", ""
|
|
|
|
} else {
|
|
|
|
keys = []string{"text"}
|
|
|
|
text = ""
|
|
|
|
}
|
2021-02-18 18:26:23 +00:00
|
|
|
} else {
|
2021-05-21 20:35:25 +00:00
|
|
|
if telegramEnabled || discordEnabled {
|
2021-05-07 15:33:44 +00:00
|
|
|
keys = []string{"html", "text", "markdown"}
|
|
|
|
} else {
|
|
|
|
keys = []string{"html", "text"}
|
|
|
|
}
|
2021-02-18 18:26:23 +00:00
|
|
|
}
|
|
|
|
for _, key := range keys {
|
2021-05-07 15:33:44 +00:00
|
|
|
var filesystem fs.FS
|
|
|
|
var fpath string
|
|
|
|
if key == "markdown" {
|
|
|
|
filesystem, fpath = app.GetPath(section, keyFragment+"text")
|
|
|
|
} else {
|
|
|
|
filesystem, fpath = app.GetPath(section, keyFragment+key)
|
|
|
|
}
|
2021-02-18 18:26:23 +00:00
|
|
|
if key == "html" {
|
|
|
|
tpl, err = template.ParseFS(filesystem, fpath)
|
|
|
|
} else {
|
|
|
|
tpl, err = textTemplate.ParseFS(filesystem, fpath)
|
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
if err != nil {
|
2021-02-02 18:09:02 +00:00
|
|
|
return
|
2021-01-30 19:19:12 +00:00
|
|
|
}
|
2021-05-07 15:33:44 +00:00
|
|
|
// For constructTemplate, if "md" is found in data it's used in stead of "text".
|
|
|
|
foundMarkdown := false
|
|
|
|
if key == "markdown" {
|
|
|
|
_, foundMarkdown = data["md"]
|
|
|
|
if foundMarkdown {
|
|
|
|
data["plaintext"], data["md"] = data["md"], data["plaintext"]
|
|
|
|
}
|
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
var tplData bytes.Buffer
|
2021-02-02 18:09:02 +00:00
|
|
|
err = tpl.Execute(&tplData, data)
|
2021-01-30 19:19:12 +00:00
|
|
|
if err != nil {
|
2021-02-02 18:09:02 +00:00
|
|
|
return
|
2021-01-30 19:19:12 +00:00
|
|
|
}
|
2021-05-07 15:33:44 +00:00
|
|
|
if foundMarkdown {
|
|
|
|
data["plaintext"], data["md"] = data["md"], data["plaintext"]
|
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
if key == "html" {
|
2021-02-02 18:09:02 +00:00
|
|
|
html = tplData.String()
|
2021-05-07 15:33:44 +00:00
|
|
|
} else if key == "text" {
|
2021-02-02 18:09:02 +00:00
|
|
|
text = tplData.String()
|
2021-05-07 15:33:44 +00:00
|
|
|
} else {
|
|
|
|
markdown = tplData.String()
|
2021-01-30 19:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) confirmationValues(code, username, key string, app *appContext, noSub bool) map[string]interface{} {
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-02-02 18:09:02 +00:00
|
|
|
"clickBelow": emailer.lang.EmailConfirmation.get("clickBelow"),
|
|
|
|
"ifItWasNotYou": emailer.lang.Strings.get("ifItWasNotYou"),
|
|
|
|
"confirmEmail": emailer.lang.EmailConfirmation.get("confirmEmail"),
|
2021-02-19 21:38:20 +00:00
|
|
|
"message": "",
|
2021-02-20 00:22:40 +00:00
|
|
|
"username": username,
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["helloUser"] = emailer.lang.Strings.get("helloUser")
|
|
|
|
empty := []string{"confirmationURL"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-07 20:53:29 +00:00
|
|
|
message := app.config.Section("messages").Key("message").String()
|
2021-02-19 21:38:20 +00:00
|
|
|
inviteLink := app.config.Section("invite_emails").Key("url_base").String()
|
|
|
|
inviteLink = fmt.Sprintf("%s/%s?key=%s", inviteLink, code, key)
|
|
|
|
template["helloUser"] = emailer.lang.Strings.template("helloUser", tmpl{"username": username})
|
|
|
|
template["confirmationURL"] = inviteLink
|
|
|
|
template["message"] = message
|
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructConfirmation(code, username, key string, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: app.config.Section("email_confirmation").Key("subject").MustString(emailer.lang.EmailConfirmation.get("title")),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
template := emailer.confirmationValues(code, username, key, app, noSub)
|
2021-02-20 00:22:40 +00:00
|
|
|
if app.storage.customEmails.EmailConfirmation.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.EmailConfirmation.Content,
|
|
|
|
app.storage.customEmails.EmailConfirmation.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "email_confirmation", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructTemplate(subject, md string, app *appContext) (*Message, error) {
|
|
|
|
email := &Message{Subject: subject}
|
2021-02-18 14:58:53 +00:00
|
|
|
renderer := html.NewRenderer(html.RendererOptions{Flags: html.Smartypants})
|
|
|
|
html := markdown.ToHTML([]byte(md), nil, renderer)
|
2021-02-20 01:03:11 +00:00
|
|
|
text := stripMarkdown(md)
|
2021-05-07 20:53:29 +00:00
|
|
|
message := app.config.Section("messages").Key("message").String()
|
2021-02-18 14:58:53 +00:00
|
|
|
var err error
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "template_email", "email_", map[string]interface{}{
|
2021-02-18 18:26:23 +00:00
|
|
|
"text": template.HTML(html),
|
|
|
|
"plaintext": text,
|
|
|
|
"message": message,
|
2021-05-07 15:33:44 +00:00
|
|
|
"md": md,
|
2021-02-18 14:58:53 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) inviteValues(code string, invite Invite, app *appContext, noSub bool) map[string]interface{} {
|
2020-07-31 11:48:37 +00:00
|
|
|
expiry := invite.ValidTill
|
2020-09-15 11:00:20 +00:00
|
|
|
d, t, expiresIn := emailer.formatExpiry(expiry, false, app.datePattern, app.timePattern)
|
2021-05-07 20:53:29 +00:00
|
|
|
message := app.config.Section("messages").Key("message").String()
|
2020-09-15 11:00:20 +00:00
|
|
|
inviteLink := app.config.Section("invite_emails").Key("url_base").String()
|
|
|
|
inviteLink = fmt.Sprintf("%s/%s", inviteLink, code)
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-02-02 18:09:02 +00:00
|
|
|
"hello": emailer.lang.InviteEmail.get("hello"),
|
|
|
|
"youHaveBeenInvited": emailer.lang.InviteEmail.get("youHaveBeenInvited"),
|
|
|
|
"toJoin": emailer.lang.InviteEmail.get("toJoin"),
|
|
|
|
"linkButton": emailer.lang.InviteEmail.get("linkButton"),
|
2021-02-19 21:38:20 +00:00
|
|
|
"message": "",
|
2021-02-20 00:22:40 +00:00
|
|
|
"date": d,
|
|
|
|
"time": t,
|
|
|
|
"expiresInMinutes": expiresIn,
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["inviteExpiry"] = emailer.lang.InviteEmail.get("inviteExpiry")
|
|
|
|
empty := []string{"inviteURL"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["inviteExpiry"] = emailer.lang.InviteEmail.template("inviteExpiry", tmpl{"date": d, "time": t, "expiresInMinutes": expiresIn})
|
|
|
|
template["inviteURL"] = inviteLink
|
|
|
|
template["message"] = message
|
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructInvite(code string, invite Invite, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-05-02 11:50:04 +00:00
|
|
|
Subject: app.config.Section("invite_emails").Key("subject").MustString(emailer.lang.InviteEmail.get("title")),
|
2021-02-22 00:43:36 +00:00
|
|
|
}
|
|
|
|
template := emailer.inviteValues(code, invite, app, noSub)
|
2021-02-19 21:38:20 +00:00
|
|
|
var err error
|
2021-02-20 00:22:40 +00:00
|
|
|
if app.storage.customEmails.InviteEmail.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.InviteEmail.Content,
|
|
|
|
app.storage.customEmails.InviteEmail.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "invite_emails", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-13 20:18:47 +00:00
|
|
|
return email, nil
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) expiryValues(code string, invite Invite, app *appContext, noSub bool) map[string]interface{} {
|
2020-08-16 12:36:54 +00:00
|
|
|
expiry := app.formatDatetime(invite.ValidTill)
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-02-02 18:09:02 +00:00
|
|
|
"inviteExpired": emailer.lang.InviteExpiry.get("inviteExpired"),
|
|
|
|
"notificationNotice": emailer.lang.InviteExpiry.get("notificationNotice"),
|
2021-02-20 00:22:40 +00:00
|
|
|
"code": "\"" + code + "\"",
|
|
|
|
"time": expiry,
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["expiredAt"] = emailer.lang.InviteExpiry.get("expiredAt")
|
|
|
|
} else {
|
2021-02-20 00:22:40 +00:00
|
|
|
template["expiredAt"] = emailer.lang.InviteExpiry.template("expiredAt", tmpl{"code": template["code"].(string), "time": template["time"].(string)})
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructExpiry(code string, invite Invite, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: emailer.lang.InviteExpiry.get("title"),
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
var err error
|
2021-02-22 00:43:36 +00:00
|
|
|
template := emailer.expiryValues(code, invite, app, noSub)
|
2021-02-20 00:22:40 +00:00
|
|
|
if app.storage.customEmails.InviteExpiry.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.InviteExpiry.Content,
|
|
|
|
app.storage.customEmails.InviteExpiry.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "notifications", "expiry_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-13 20:18:47 +00:00
|
|
|
return email, nil
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) createdValues(code, username, address string, invite Invite, app *appContext, noSub bool) map[string]interface{} {
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
|
|
|
"nameString": emailer.lang.Strings.get("name"),
|
|
|
|
"addressString": emailer.lang.Strings.get("emailAddress"),
|
|
|
|
"timeString": emailer.lang.UserCreated.get("time"),
|
|
|
|
"notificationNotice": "",
|
2021-02-20 00:22:40 +00:00
|
|
|
"code": "\"" + code + "\"",
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["aUserWasCreated"] = emailer.lang.UserCreated.get("aUserWasCreated")
|
|
|
|
empty := []string{"name", "address", "time"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
2020-07-31 11:48:37 +00:00
|
|
|
} else {
|
2021-02-19 21:38:20 +00:00
|
|
|
created := app.formatDatetime(invite.Created)
|
|
|
|
var tplAddress string
|
|
|
|
if app.config.Section("email").Key("no_username").MustBool(false) {
|
|
|
|
tplAddress = "n/a"
|
|
|
|
} else {
|
|
|
|
tplAddress = address
|
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
template["aUserWasCreated"] = emailer.lang.UserCreated.template("aUserWasCreated", tmpl{"code": template["code"].(string)})
|
2021-02-19 21:38:20 +00:00
|
|
|
template["name"] = username
|
|
|
|
template["address"] = tplAddress
|
|
|
|
template["time"] = created
|
|
|
|
template["notificationNotice"] = emailer.lang.UserCreated.get("notificationNotice")
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructCreated(code, username, address string, invite Invite, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: emailer.lang.UserCreated.get("title"),
|
|
|
|
}
|
|
|
|
template := emailer.createdValues(code, username, address, invite, app, noSub)
|
2021-02-02 18:09:02 +00:00
|
|
|
var err error
|
2021-02-20 00:22:40 +00:00
|
|
|
if app.storage.customEmails.UserCreated.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.UserCreated.Content,
|
|
|
|
app.storage.customEmails.UserCreated.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "notifications", "created_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-13 20:18:47 +00:00
|
|
|
return email, nil
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) resetValues(pwr PasswordReset, app *appContext, noSub bool) map[string]interface{} {
|
2020-09-15 11:00:20 +00:00
|
|
|
d, t, expiresIn := emailer.formatExpiry(pwr.Expiry, true, app.datePattern, app.timePattern)
|
2021-05-07 20:53:29 +00:00
|
|
|
message := app.config.Section("messages").Key("message").String()
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-02-02 18:09:02 +00:00
|
|
|
"someoneHasRequestedReset": emailer.lang.PasswordReset.get("someoneHasRequestedReset"),
|
|
|
|
"ifItWasNotYou": emailer.lang.Strings.get("ifItWasNotYou"),
|
2021-02-19 21:38:20 +00:00
|
|
|
"pinString": emailer.lang.PasswordReset.get("pin"),
|
2021-03-30 21:41:28 +00:00
|
|
|
"link_reset": false,
|
2021-02-19 21:38:20 +00:00
|
|
|
"message": "",
|
2021-02-20 00:22:40 +00:00
|
|
|
"username": pwr.Username,
|
|
|
|
"date": d,
|
|
|
|
"time": t,
|
|
|
|
"expiresInMinutes": expiresIn,
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-03-30 21:41:28 +00:00
|
|
|
linkResetEnabled := app.config.Section("password_resets").Key("link_reset").MustBool(false)
|
|
|
|
if linkResetEnabled {
|
|
|
|
template["ifItWasYou"] = emailer.lang.PasswordReset.get("ifItWasYouLink")
|
|
|
|
} else {
|
|
|
|
template["ifItWasYou"] = emailer.lang.PasswordReset.get("ifItWasYou")
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
if noSub {
|
|
|
|
template["helloUser"] = emailer.lang.Strings.get("helloUser")
|
|
|
|
template["codeExpiry"] = emailer.lang.PasswordReset.get("codeExpiry")
|
|
|
|
empty := []string{"pin"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["helloUser"] = emailer.lang.Strings.template("helloUser", tmpl{"username": pwr.Username})
|
|
|
|
template["codeExpiry"] = emailer.lang.PasswordReset.template("codeExpiry", tmpl{"date": d, "time": t, "expiresInMinutes": expiresIn})
|
2021-03-30 21:41:28 +00:00
|
|
|
inviteLink := app.config.Section("invite_emails").Key("url_base").String()
|
|
|
|
if linkResetEnabled {
|
|
|
|
if inviteLink != "" {
|
|
|
|
// Strip /invite form end of this URL, ik its ugly.
|
|
|
|
template["link_reset"] = true
|
|
|
|
pinLink := fmt.Sprintf("%s/reset?pin=%s", strings.Replace(inviteLink, "/invite", "", 1), pwr.Pin)
|
|
|
|
template["pin"] = pinLink
|
|
|
|
// Only used in html email.
|
|
|
|
template["pin_code"] = pwr.Pin
|
|
|
|
} else {
|
|
|
|
app.info.Println("Password Reset link disabled as no URL Base provided. Set in Settings > Invite Emails.")
|
|
|
|
template["pin"] = pwr.Pin
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["pin"] = pwr.Pin
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
template["message"] = message
|
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: app.config.Section("password_resets").Key("subject").MustString(emailer.lang.PasswordReset.get("title")),
|
|
|
|
}
|
|
|
|
template := emailer.resetValues(pwr, app, noSub)
|
2021-02-19 21:38:20 +00:00
|
|
|
var err error
|
2021-02-20 00:22:40 +00:00
|
|
|
if app.storage.customEmails.PasswordReset.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.PasswordReset.Content,
|
|
|
|
app.storage.customEmails.PasswordReset.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "password_resets", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
2020-09-13 20:18:47 +00:00
|
|
|
return email, nil
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) deletedValues(reason string, app *appContext, noSub bool) map[string]interface{} {
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-04-12 20:28:36 +00:00
|
|
|
"yourAccountWas": emailer.lang.UserDeleted.get("yourAccountWasDeleted"),
|
|
|
|
"reasonString": emailer.lang.Strings.get("reason"),
|
|
|
|
"message": "",
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
empty := []string{"reason"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["reason"] = reason
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructDeleted(reason string, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: app.config.Section("deletion").Key("subject").MustString(emailer.lang.UserDeleted.get("title")),
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
var err error
|
2021-02-22 00:43:36 +00:00
|
|
|
template := emailer.deletedValues(reason, app, noSub)
|
2021-02-20 00:22:40 +00:00
|
|
|
if app.storage.customEmails.UserDeleted.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.UserDeleted.Content,
|
|
|
|
app.storage.customEmails.UserDeleted.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "deletion", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-17 22:50:07 +00:00
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-04-12 20:28:36 +00:00
|
|
|
func (emailer *Emailer) disabledValues(reason string, app *appContext, noSub bool) map[string]interface{} {
|
|
|
|
template := map[string]interface{}{
|
|
|
|
"yourAccountWas": emailer.lang.UserDisabled.get("yourAccountWasDisabled"),
|
|
|
|
"reasonString": emailer.lang.Strings.get("reason"),
|
|
|
|
"message": "",
|
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
empty := []string{"reason"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["reason"] = reason
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructDisabled(reason string, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-04-12 20:28:36 +00:00
|
|
|
Subject: app.config.Section("disable_enable").Key("subject_disabled").MustString(emailer.lang.UserDisabled.get("title")),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
template := emailer.disabledValues(reason, app, noSub)
|
|
|
|
if app.storage.customEmails.UserDisabled.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.UserDisabled.Content,
|
|
|
|
app.storage.customEmails.UserDisabled.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-04-12 20:28:36 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "disable_enable", "disabled_", template)
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (emailer *Emailer) enabledValues(reason string, app *appContext, noSub bool) map[string]interface{} {
|
|
|
|
template := map[string]interface{}{
|
|
|
|
"yourAccountWas": emailer.lang.UserEnabled.get("yourAccountWasEnabled"),
|
|
|
|
"reasonString": emailer.lang.Strings.get("reason"),
|
|
|
|
"message": "",
|
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
empty := []string{"reason"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["reason"] = reason
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructEnabled(reason string, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-04-12 20:28:36 +00:00
|
|
|
Subject: app.config.Section("disable_enable").Key("subject_enabled").MustString(emailer.lang.UserEnabled.get("title")),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
template := emailer.enabledValues(reason, app, noSub)
|
|
|
|
if app.storage.customEmails.UserEnabled.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.UserEnabled.Content,
|
|
|
|
app.storage.customEmails.UserEnabled.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-04-12 20:28:36 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "disable_enable", "enabled_", template)
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:20:13 +00:00
|
|
|
func (emailer *Emailer) welcomeValues(username string, expiry time.Time, app *appContext, noSub bool, custom bool) map[string]interface{} {
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-05-02 13:44:19 +00:00
|
|
|
"welcome": emailer.lang.WelcomeEmail.get("welcome"),
|
|
|
|
"youCanLoginWith": emailer.lang.WelcomeEmail.get("youCanLoginWith"),
|
|
|
|
"jellyfinURLString": emailer.lang.WelcomeEmail.get("jellyfinURL"),
|
|
|
|
"usernameString": emailer.lang.Strings.get("username"),
|
|
|
|
"message": "",
|
|
|
|
"yourAccountWillExpire": "",
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
2021-04-08 13:20:13 +00:00
|
|
|
empty := []string{"jellyfinURL", "username", "yourAccountWillExpire"}
|
2021-02-19 21:38:20 +00:00
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["jellyfinURL"] = app.config.Section("jellyfin").Key("public_server").String()
|
|
|
|
template["username"] = username
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-04-08 13:20:13 +00:00
|
|
|
exp := app.formatDatetime(expiry)
|
2021-04-15 14:34:17 +00:00
|
|
|
if !expiry.IsZero() {
|
|
|
|
if custom {
|
|
|
|
template["yourAccountWillExpire"] = exp
|
|
|
|
} else if !expiry.IsZero() {
|
|
|
|
template["yourAccountWillExpire"] = emailer.lang.WelcomeEmail.template("yourAccountWillExpire", tmpl{
|
|
|
|
"date": exp,
|
|
|
|
})
|
|
|
|
}
|
2021-04-08 13:20:13 +00:00
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructWelcome(username string, expiry time.Time, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: app.config.Section("welcome_email").Key("subject").MustString(emailer.lang.WelcomeEmail.get("title")),
|
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
var err error
|
2021-04-08 13:20:13 +00:00
|
|
|
var template map[string]interface{}
|
|
|
|
if app.storage.customEmails.WelcomeEmail.Enabled {
|
|
|
|
template = emailer.welcomeValues(username, expiry, app, noSub, true)
|
|
|
|
} else {
|
|
|
|
template = emailer.welcomeValues(username, expiry, app, noSub, false)
|
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["yourAccountWillExpire"] = emailer.lang.WelcomeEmail.template("yourAccountWillExpire", tmpl{
|
|
|
|
"date": "{yourAccountWillExpire}",
|
|
|
|
})
|
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
if app.storage.customEmails.WelcomeEmail.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.WelcomeEmail.Content,
|
|
|
|
app.storage.customEmails.WelcomeEmail.Variables,
|
|
|
|
app.storage.customEmails.WelcomeEmail.Conditionals,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "welcome_email", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-01-24 15:19:58 +00:00
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 15:41:06 +00:00
|
|
|
func (emailer *Emailer) userExpiredValues(app *appContext, noSub bool) map[string]interface{} {
|
|
|
|
template := map[string]interface{}{
|
|
|
|
"yourAccountHasExpired": emailer.lang.UserExpired.get("yourAccountHasExpired"),
|
|
|
|
"contactTheAdmin": emailer.lang.UserExpired.get("contactTheAdmin"),
|
|
|
|
"message": "",
|
|
|
|
}
|
|
|
|
if !noSub {
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-02-28 15:41:06 +00:00
|
|
|
}
|
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructUserExpired(app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-28 15:41:06 +00:00
|
|
|
Subject: app.config.Section("user_expiry").Key("subject").MustString(emailer.lang.UserExpired.get("title")),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
template := emailer.userExpiredValues(app, noSub)
|
|
|
|
if app.storage.customEmails.UserExpired.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
|
|
|
app.storage.customEmails.UserExpired.Content,
|
|
|
|
app.storage.customEmails.UserExpired.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-28 15:41:06 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "user_expiry", "email_", template)
|
2021-02-28 15:41:06 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// calls the send method in the underlying emailClient.
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) send(email *Message, address ...string) error {
|
|
|
|
return emailer.sender.Send(emailer.fromName, emailer.fromAddr, email, address...)
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2021-05-07 15:06:47 +00:00
|
|
|
|
|
|
|
func (app *appContext) sendByID(email *Message, ID ...string) error {
|
|
|
|
for _, id := range ID {
|
|
|
|
var err error
|
2021-05-07 20:53:29 +00:00
|
|
|
if tgChat, ok := app.storage.telegram[id]; ok && tgChat.Contact && telegramEnabled {
|
2021-05-07 15:06:47 +00:00
|
|
|
err = app.telegram.Send(email, tgChat.ChatID)
|
2021-05-21 20:35:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dcChat, ok := app.storage.discord[id]; ok && dcChat.Contact && discordEnabled {
|
|
|
|
err = app.discord.Send(email, dcChat.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 21:46:46 +00:00
|
|
|
if address, ok := app.storage.emails[id]; ok && address.Contact && emailEnabled {
|
|
|
|
err = app.email.send(email, address.Addr)
|
2021-05-21 20:35:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-07 15:06:47 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) getAddressOrName(jfID string) string {
|
2021-05-21 20:35:25 +00:00
|
|
|
if dcChat, ok := app.storage.discord[jfID]; ok && dcChat.Contact && discordEnabled {
|
|
|
|
return dcChat.Username + "#" + dcChat.Discriminator
|
|
|
|
}
|
2021-05-07 20:53:29 +00:00
|
|
|
if tgChat, ok := app.storage.telegram[jfID]; ok && tgChat.Contact && telegramEnabled {
|
2021-05-07 15:06:47 +00:00
|
|
|
return "@" + tgChat.Username
|
|
|
|
}
|
|
|
|
if addr, ok := app.storage.emails[jfID]; ok {
|
2021-05-21 21:46:46 +00:00
|
|
|
return addr.Addr
|
2021-05-07 15:06:47 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|