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
|
|
|
)
|
|
|
|
|
2020-09-13 20:07:15 +00:00
|
|
|
// implements email sending, right now via smtp or mailgun.
|
|
|
|
type emailClient interface {
|
2020-09-13 20:18:47 +00:00
|
|
|
send(address, fromName, fromAddr string, email *Email) error
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-13 20:18:47 +00:00
|
|
|
func (mg *Mailgun) send(address, fromName, fromAddr string, email *Email) error {
|
2020-09-13 20:07:15 +00:00
|
|
|
message := mg.client.NewMessage(
|
|
|
|
fmt.Sprintf("%s <%s>", fromName, fromAddr),
|
|
|
|
email.subject,
|
|
|
|
email.text,
|
|
|
|
address,
|
|
|
|
)
|
|
|
|
message.SetHtml(email.html)
|
|
|
|
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 {
|
2020-11-14 15:33:25 +00:00
|
|
|
sslTLS bool
|
|
|
|
server string
|
|
|
|
port int
|
|
|
|
auth smtp.Auth
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
func (sm *SMTP) send(address, fromName, fromAddr string, email *Email) error {
|
2020-09-13 20:07:15 +00:00
|
|
|
e := jEmail.NewEmail()
|
|
|
|
e.Subject = email.subject
|
|
|
|
e.From = fmt.Sprintf("%s <%s>", fromName, fromAddr)
|
|
|
|
e.To = []string{address}
|
|
|
|
e.Text = []byte(email.text)
|
|
|
|
e.HTML = []byte(email.html)
|
2020-11-14 15:33:25 +00:00
|
|
|
server := fmt.Sprintf("%s:%d", sm.server, sm.port)
|
2020-09-13 20:07:15 +00:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: false,
|
2020-11-14 15:33:25 +00:00
|
|
|
ServerName: sm.server,
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
var err error
|
2020-11-14 15:33:25 +00:00
|
|
|
// err = e.Send(server, sm.auth)
|
2020-09-15 11:00:20 +00:00
|
|
|
if sm.sslTLS {
|
2020-09-13 20:07:15 +00:00
|
|
|
err = e.SendWithTLS(server, sm.auth, tlsConfig)
|
|
|
|
} else {
|
|
|
|
err = e.SendWithStartTLS(server, sm.auth, tlsConfig)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emailer contains the email sender, email content, and methods to construct message content.
|
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
|
2020-09-13 20:07:15 +00:00
|
|
|
sender emailClient
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 20:18:47 +00:00
|
|
|
// Email stores content.
|
|
|
|
type Email struct {
|
2020-07-31 11:48:37 +00:00
|
|
|
subject string
|
|
|
|
html, text string
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
func (emailer *Emailer) formatExpiry(expiry time.Time, tzaware bool, datePattern, timePattern string) (d, t, expiresIn string) {
|
2020-07-31 11:48:37 +00:00
|
|
|
d, _ = strtime.Strftime(expiry, datePattern)
|
|
|
|
t, _ = strtime.Strftime(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 {
|
2020-09-15 11:00:20 +00:00
|
|
|
expiresIn += fmt.Sprintf("%dd ", days)
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if hours != 0 {
|
2020-09-15 11:00:20 +00:00
|
|
|
expiresIn += fmt.Sprintf("%dh ", hours)
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if minutes != 0 {
|
2020-09-15 11:00:20 +00:00
|
|
|
expiresIn += fmt.Sprintf("%dm ", minutes)
|
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
|
|
|
|
}
|
2020-11-14 15:33:25 +00:00
|
|
|
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)
|
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())
|
|
|
|
}
|
|
|
|
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.
|
2020-11-14 15:33:25 +00:00
|
|
|
func (emailer *Emailer) NewSMTP(server string, port int, username, password string, sslTLS bool) {
|
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,
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 20:18:47 +00:00
|
|
|
func (emailer *Emailer) constructInvite(code string, invite Invite, app *appContext) (*Email, error) {
|
|
|
|
email := &Email{
|
2021-01-19 00:29:29 +00:00
|
|
|
subject: app.config.Section("invite_emails").Key("subject").MustString(emailer.lang.InviteEmail.get("title")),
|
2020-09-13 20:18:47 +00:00
|
|
|
}
|
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)
|
2020-08-16 12:36:54 +00:00
|
|
|
message := app.config.Section("email").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)
|
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("invite_emails").Key("email_" + key).String()
|
2020-07-31 11:48:37 +00:00
|
|
|
tpl, err := template.ParseFiles(fpath)
|
|
|
|
if err != nil {
|
2020-09-13 20:18:47 +00:00
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
var tplData bytes.Buffer
|
|
|
|
err = tpl.Execute(&tplData, map[string]string{
|
2021-01-19 00:29:29 +00:00
|
|
|
"hello": emailer.lang.InviteEmail.get("hello"),
|
|
|
|
"youHaveBeenInvited": emailer.lang.InviteEmail.get("youHaveBeenInvited"),
|
|
|
|
"toJoin": emailer.lang.InviteEmail.get("toJoin"),
|
|
|
|
"inviteExpiry": emailer.lang.InviteEmail.format("inviteExpiry", d, t, expiresIn),
|
|
|
|
"linkButton": emailer.lang.InviteEmail.get("linkButton"),
|
2021-01-14 17:51:12 +00:00
|
|
|
"invite_link": inviteLink,
|
|
|
|
"message": message,
|
2020-07-31 11:48:37 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-13 20:18:47 +00:00
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if key == "html" {
|
2020-09-13 20:18:47 +00:00
|
|
|
email.html = tplData.String()
|
2020-07-31 11:48:37 +00:00
|
|
|
} else {
|
2020-09-13 20:18:47 +00:00
|
|
|
email.text = tplData.String()
|
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
|
|
|
}
|
|
|
|
|
2020-09-13 20:18:47 +00:00
|
|
|
func (emailer *Emailer) constructExpiry(code string, invite Invite, app *appContext) (*Email, error) {
|
|
|
|
email := &Email{
|
2021-01-19 00:29:29 +00:00
|
|
|
subject: emailer.lang.InviteExpiry.get("title"),
|
2020-09-13 20:18:47 +00:00
|
|
|
}
|
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 {
|
2020-09-13 20:18:47 +00:00
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
var tplData bytes.Buffer
|
|
|
|
err = tpl.Execute(&tplData, map[string]string{
|
2021-01-19 00:29:29 +00:00
|
|
|
"inviteExpired": emailer.lang.InviteExpiry.get("inviteExpired"),
|
|
|
|
"expiredAt": emailer.lang.InviteExpiry.format("expiredAt", "\""+code+"\"", expiry),
|
|
|
|
"notificationNotice": emailer.lang.InviteExpiry.get("notificationNotice"),
|
2020-07-31 11:48:37 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-13 20:18:47 +00:00
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if key == "html" {
|
2020-09-13 20:18:47 +00:00
|
|
|
email.html = tplData.String()
|
2020-07-31 11:48:37 +00:00
|
|
|
} else {
|
2020-09-13 20:18:47 +00:00
|
|
|
email.text = tplData.String()
|
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
|
|
|
}
|
|
|
|
|
2020-09-13 20:18:47 +00:00
|
|
|
func (emailer *Emailer) constructCreated(code, username, address string, invite Invite, app *appContext) (*Email, error) {
|
|
|
|
email := &Email{
|
2021-01-19 00:29:29 +00:00
|
|
|
subject: emailer.lang.UserCreated.get("title"),
|
2020-09-13 20:18:47 +00:00
|
|
|
}
|
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 {
|
2020-09-13 20:18:47 +00:00
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
var tplData bytes.Buffer
|
|
|
|
err = tpl.Execute(&tplData, map[string]string{
|
2021-01-19 00:29:29 +00:00
|
|
|
"aUserWasCreated": emailer.lang.UserCreated.format("aUserWasCreated", "\""+code+"\""),
|
|
|
|
"name": emailer.lang.UserCreated.get("name"),
|
|
|
|
"address": emailer.lang.UserCreated.get("emailAddress"),
|
|
|
|
"time": emailer.lang.UserCreated.get("time"),
|
2021-01-14 17:51:12 +00:00
|
|
|
"nameVal": username,
|
|
|
|
"addressVal": tplAddress,
|
|
|
|
"timeVal": created,
|
2021-01-19 00:29:29 +00:00
|
|
|
"notificationNotice": emailer.lang.UserCreated.get("notificationNotice"),
|
2020-07-31 11:48:37 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-13 20:18:47 +00:00
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if key == "html" {
|
2020-09-13 20:18:47 +00:00
|
|
|
email.html = tplData.String()
|
2020-07-31 11:48:37 +00:00
|
|
|
} else {
|
2020-09-13 20:18:47 +00:00
|
|
|
email.text = tplData.String()
|
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
|
|
|
}
|
|
|
|
|
2020-11-22 16:36:43 +00:00
|
|
|
func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext) (*Email, error) {
|
2020-09-13 20:18:47 +00:00
|
|
|
email := &Email{
|
2021-01-19 00:29:29 +00:00
|
|
|
subject: emailer.lang.PasswordReset.get("title"),
|
2020-09-13 20:18:47 +00:00
|
|
|
}
|
2020-09-15 11:00:20 +00:00
|
|
|
d, t, expiresIn := emailer.formatExpiry(pwr.Expiry, true, app.datePattern, app.timePattern)
|
2020-08-16 12:36:54 +00:00
|
|
|
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 {
|
2020-09-13 20:18:47 +00:00
|
|
|
return nil, err
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
var tplData bytes.Buffer
|
|
|
|
err = tpl.Execute(&tplData, map[string]string{
|
2021-01-19 00:29:29 +00:00
|
|
|
"helloUser": emailer.lang.PasswordReset.format("helloUser", pwr.Username),
|
|
|
|
"someoneHasRequestedReset": emailer.lang.PasswordReset.get("someoneHasRequestedReset"),
|
|
|
|
"ifItWasYou": emailer.lang.PasswordReset.get("ifItWasYou"),
|
|
|
|
"codeExpiry": emailer.lang.PasswordReset.format("codeExpiry", d, t, expiresIn),
|
|
|
|
"ifItWasNotYou": emailer.lang.PasswordReset.get("ifItWasNotYou"),
|
|
|
|
"pin": emailer.lang.PasswordReset.get("pin"),
|
2021-01-14 17:51:12 +00:00
|
|
|
"pinVal": pwr.Pin,
|
|
|
|
"message": message,
|
2020-08-01 15:31:08 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-13 20:18:47 +00:00
|
|
|
return nil, err
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
if key == "html" {
|
2020-09-13 20:18:47 +00:00
|
|
|
email.html = tplData.String()
|
2020-08-01 15:31:08 +00:00
|
|
|
} else {
|
2020-09-13 20:18:47 +00:00
|
|
|
email.text = tplData.String()
|
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
|
|
|
}
|
|
|
|
|
2020-09-17 22:50:07 +00:00
|
|
|
func (emailer *Emailer) constructDeleted(reason string, app *appContext) (*Email, error) {
|
|
|
|
email := &Email{
|
2021-01-19 00:29:29 +00:00
|
|
|
subject: emailer.lang.UserDeleted.get("title"),
|
2020-09-17 22:50:07 +00:00
|
|
|
}
|
|
|
|
for _, key := range []string{"html", "text"} {
|
|
|
|
fpath := app.config.Section("deletion").Key("email_" + key).String()
|
|
|
|
tpl, err := template.ParseFiles(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tplData bytes.Buffer
|
|
|
|
err = tpl.Execute(&tplData, map[string]string{
|
2021-01-19 00:29:29 +00:00
|
|
|
"yourAccountWasDeleted": emailer.lang.UserDeleted.get("yourAccountWasDeleted"),
|
|
|
|
"reason": emailer.lang.UserDeleted.get("reason"),
|
2021-01-14 17:51:12 +00:00
|
|
|
"reasonVal": reason,
|
2020-09-17 22:50:07 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if key == "html" {
|
|
|
|
email.html = tplData.String()
|
|
|
|
} else {
|
|
|
|
email.text = tplData.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// calls the send method in the underlying emailClient.
|
2020-09-13 20:18:47 +00:00
|
|
|
func (emailer *Emailer) send(address string, email *Email) error {
|
|
|
|
return emailer.sender.send(address, emailer.fromName, emailer.fromAddr, email)
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|