2020-07-31 11:48:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-08-02 16:20:50 +00:00
|
|
|
"crypto/tls"
|
2020-07-31 11:48:37 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/smtp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2020-08-16 12:36:54 +00:00
|
|
|
|
|
|
|
jEmail "github.com/jordan-wright/email"
|
|
|
|
"github.com/knz/strtime"
|
|
|
|
"github.com/mailgun/mailgun-go/v4"
|
2020-07-31 11:48:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Emailer struct {
|
|
|
|
smtpAuth smtp.Auth
|
|
|
|
sendType, sendMethod, fromAddr, fromName string
|
|
|
|
content Email
|
|
|
|
mg *mailgun.MailgunImpl
|
2020-08-02 16:20:50 +00:00
|
|
|
mime string
|
|
|
|
host string
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Email struct {
|
|
|
|
subject string
|
|
|
|
html, text string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (email *Emailer) formatExpiry(expiry time.Time, tzaware bool, datePattern, timePattern string) (d, t, expires_in string) {
|
|
|
|
d, _ = strtime.Strftime(expiry, datePattern)
|
|
|
|
t, _ = strtime.Strftime(expiry, timePattern)
|
|
|
|
current_time := time.Now()
|
|
|
|
if tzaware {
|
|
|
|
current_time = current_time.UTC()
|
|
|
|
}
|
|
|
|
_, _, days, hours, minutes, _ := timeDiff(expiry, current_time)
|
|
|
|
if days != 0 {
|
|
|
|
expires_in += fmt.Sprintf("%dd ", days)
|
|
|
|
}
|
|
|
|
if hours != 0 {
|
|
|
|
expires_in += fmt.Sprintf("%dh ", hours)
|
|
|
|
}
|
|
|
|
if minutes != 0 {
|
|
|
|
expires_in += fmt.Sprintf("%dm ", minutes)
|
|
|
|
}
|
|
|
|
expires_in = strings.TrimSuffix(expires_in, " ")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (email *Emailer) init(app *appContext) {
|
|
|
|
email.fromAddr = app.config.Section("email").Key("address").String()
|
|
|
|
email.fromName = app.config.Section("email").Key("from").String()
|
|
|
|
email.sendMethod = app.config.Section("email").Key("method").String()
|
2020-07-31 11:48:37 +00:00
|
|
|
if email.sendMethod == "mailgun" {
|
2020-08-16 12:36:54 +00:00
|
|
|
email.mg = mailgun.NewMailgun(strings.Split(email.fromAddr, "@")[1], app.config.Section("mailgun").Key("api_key").String())
|
|
|
|
api_url := app.config.Section("mailgun").Key("api_url").String()
|
2020-08-02 16:20:50 +00:00
|
|
|
// Mailgun client takes the base url, so we need to trim off the end (e.g 'v3/messages'
|
2020-07-31 11:48:37 +00:00
|
|
|
if strings.Contains(api_url, "messages") {
|
|
|
|
api_url = api_url[0:strings.LastIndex(api_url, "/")]
|
|
|
|
api_url = api_url[0:strings.LastIndex(api_url, "/")]
|
|
|
|
}
|
|
|
|
email.mg.SetAPIBase(api_url)
|
2020-08-02 16:20:50 +00:00
|
|
|
} else if email.sendMethod == "smtp" {
|
2020-08-16 12:36:54 +00:00
|
|
|
app.host = app.config.Section("smtp").Key("server").String()
|
|
|
|
email.smtpAuth = smtp.PlainAuth("", email.fromAddr, app.config.Section("smtp").Key("password").String(), app.host)
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (email *Emailer) constructInvite(code string, invite Invite, app *appContext) error {
|
|
|
|
email.content.subject = app.config.Section("invite_emails").Key("subject").String()
|
2020-07-31 11:48:37 +00:00
|
|
|
expiry := invite.ValidTill
|
2020-08-16 12:36:54 +00:00
|
|
|
d, t, expires_in := email.formatExpiry(expiry, false, app.datePattern, app.timePattern)
|
|
|
|
message := app.config.Section("email").Key("message").String()
|
|
|
|
invite_link := app.config.Section("invite_emails").Key("url_base").String()
|
2020-07-31 11:48:37 +00:00
|
|
|
invite_link = fmt.Sprintf("%s/%s", invite_link, code)
|
|
|
|
|
|
|
|
for _, key := range []string{"html", "text"} {
|
2020-08-16 12:36:54 +00:00
|
|
|
fpath := app.config.Section("invite_emails").Key("email_" + key).String()
|
2020-07-31 11:48:37 +00:00
|
|
|
tpl, err := template.ParseFiles(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var tplData bytes.Buffer
|
|
|
|
err = tpl.Execute(&tplData, map[string]string{
|
|
|
|
"expiry_date": d,
|
|
|
|
"expiry_time": t,
|
|
|
|
"expires_in": expires_in,
|
|
|
|
"invite_link": invite_link,
|
|
|
|
"message": message,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if key == "html" {
|
|
|
|
email.content.html = tplData.String()
|
|
|
|
} else {
|
|
|
|
email.content.text = tplData.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
email.sendType = "invite"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (email *Emailer) constructExpiry(code string, invite Invite, app *appContext) error {
|
2020-07-31 11:48:37 +00:00
|
|
|
email.content.subject = "Notice: Invite expired"
|
2020-08-16 12:36:54 +00:00
|
|
|
expiry := app.formatDatetime(invite.ValidTill)
|
2020-07-31 11:48:37 +00:00
|
|
|
for _, key := range []string{"html", "text"} {
|
2020-08-16 12:36:54 +00:00
|
|
|
fpath := app.config.Section("notifications").Key("expiry_" + key).String()
|
2020-07-31 11:48:37 +00:00
|
|
|
tpl, err := template.ParseFiles(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var tplData bytes.Buffer
|
|
|
|
err = tpl.Execute(&tplData, map[string]string{
|
|
|
|
"code": code,
|
|
|
|
"expiry": expiry,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if key == "html" {
|
|
|
|
email.content.html = tplData.String()
|
|
|
|
} else {
|
|
|
|
email.content.text = tplData.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
email.sendType = "expiry"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (email *Emailer) constructCreated(code, username, address string, invite Invite, app *appContext) error {
|
2020-07-31 11:48:37 +00:00
|
|
|
email.content.subject = "Notice: User created"
|
2020-08-16 12:36:54 +00:00
|
|
|
created := app.formatDatetime(invite.Created)
|
2020-07-31 11:48:37 +00:00
|
|
|
var tplAddress string
|
2020-08-16 12:36:54 +00:00
|
|
|
if app.config.Section("email").Key("no_username").MustBool(false) {
|
2020-07-31 11:48:37 +00:00
|
|
|
tplAddress = "n/a"
|
|
|
|
} else {
|
|
|
|
tplAddress = address
|
|
|
|
}
|
|
|
|
for _, key := range []string{"html", "text"} {
|
2020-08-16 12:36:54 +00:00
|
|
|
fpath := app.config.Section("notifications").Key("created_" + key).String()
|
2020-07-31 11:48:37 +00:00
|
|
|
tpl, err := template.ParseFiles(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var tplData bytes.Buffer
|
|
|
|
err = tpl.Execute(&tplData, map[string]string{
|
|
|
|
"code": code,
|
|
|
|
"username": username,
|
|
|
|
"address": tplAddress,
|
|
|
|
"time": created,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if key == "html" {
|
|
|
|
email.content.html = tplData.String()
|
|
|
|
} else {
|
|
|
|
email.content.text = tplData.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
email.sendType = "created"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (email *Emailer) constructReset(pwr Pwr, app *appContext) error {
|
|
|
|
email.content.subject = app.config.Section("password_resets").Key("subject").MustString("Password reset - Jellyfin")
|
|
|
|
d, t, expires_in := email.formatExpiry(pwr.Expiry, true, app.datePattern, app.timePattern)
|
|
|
|
message := app.config.Section("email").Key("message").String()
|
2020-08-01 15:31:08 +00:00
|
|
|
for _, key := range []string{"html", "text"} {
|
2020-08-16 12:36:54 +00:00
|
|
|
fpath := app.config.Section("password_resets").Key("email_" + key).String()
|
2020-08-01 15:31:08 +00:00
|
|
|
tpl, err := template.ParseFiles(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var tplData bytes.Buffer
|
|
|
|
err = tpl.Execute(&tplData, map[string]string{
|
|
|
|
"username": pwr.Username,
|
|
|
|
"expiry_date": d,
|
|
|
|
"expiry_time": t,
|
|
|
|
"expires_in": expires_in,
|
|
|
|
"pin": pwr.Pin,
|
|
|
|
"message": message,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if key == "html" {
|
|
|
|
email.content.html = tplData.String()
|
|
|
|
} else {
|
|
|
|
email.content.text = tplData.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
email.sendType = "reset"
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-16 12:36:54 +00:00
|
|
|
func (email *Emailer) send(address string, app *appContext) error {
|
2020-07-31 11:48:37 +00:00
|
|
|
if email.sendMethod == "mailgun" {
|
|
|
|
message := email.mg.NewMessage(
|
|
|
|
fmt.Sprintf("%s <%s>", email.fromName, email.fromAddr),
|
|
|
|
email.content.subject,
|
|
|
|
email.content.text,
|
|
|
|
address)
|
|
|
|
message.SetHtml(email.content.html)
|
2020-08-16 12:36:54 +00:00
|
|
|
mgapp, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
2020-07-31 11:48:37 +00:00
|
|
|
defer cancel()
|
2020-08-16 12:36:54 +00:00
|
|
|
_, _, err := email.mg.Send(mgapp, message)
|
2020-08-01 14:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-02 16:20:50 +00:00
|
|
|
} else if email.sendMethod == "smtp" {
|
|
|
|
e := jEmail.NewEmail()
|
|
|
|
e.Subject = email.content.subject
|
|
|
|
e.From = fmt.Sprintf("%s <%s>", email.fromName, email.fromAddr)
|
|
|
|
e.To = []string{address}
|
|
|
|
e.Text = []byte(email.content.text)
|
|
|
|
e.HTML = []byte(email.content.html)
|
2020-08-16 12:36:54 +00:00
|
|
|
smtpType := app.config.Section("smtp").Key("encryption").String()
|
2020-08-02 16:20:50 +00:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: false,
|
2020-08-16 12:36:54 +00:00
|
|
|
ServerName: app.host,
|
2020-08-02 16:20:50 +00:00
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if smtpType == "ssl_tls" {
|
2020-08-16 12:36:54 +00:00
|
|
|
port := app.config.Section("smtp").Key("port").MustInt(465)
|
|
|
|
server := fmt.Sprintf("%s:%d", app.host, port)
|
2020-08-02 16:20:50 +00:00
|
|
|
err = e.SendWithTLS(server, email.smtpAuth, tlsConfig)
|
|
|
|
} else if smtpType == "starttls" {
|
2020-08-16 12:36:54 +00:00
|
|
|
port := app.config.Section("smtp").Key("port").MustInt(587)
|
|
|
|
server := fmt.Sprintf("%s:%d", app.host, port)
|
2020-08-02 16:20:50 +00:00
|
|
|
e.SendWithStartTLS(server, email.smtpAuth, tlsConfig)
|
|
|
|
}
|
|
|
|
return err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|