1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package email
import (
"bytes"
_ "embed"
"fmt"
"net/smtp"
"strings"
"text/template"
"github.com/owncast/owncast/core/data"
"github.com/pkg/errors"
)
//go:embed "golive.tmpl.html"
var goLiveTemplate string
// Email represents an instance of the Email notifier.
type Email struct {
From string
SMTPServer string
SMTPPort string
Username string
Password string
}
// New creates a new instance of the Email notifier.
func new(from, server, port, username, password string) *Email {
return &Email{
From: from,
SMTPServer: server,
SMTPPort: port,
Username: username,
Password: password,
}
}
// New creates a new instance of the email notifier.
func New() (*Email, error) {
smtpConfig := data.GetSMTPConfiguration()
if smtpConfig.Enabled && smtpConfig.FromAddress != "" {
e := new(smtpConfig.FromAddress, smtpConfig.Server, "587", smtpConfig.Username, smtpConfig.Password)
return e, nil
}
return nil, errors.New("email delivery not configured")
}
// Send will send an email notification.
func (e *Email) Send(to []string, content, subject string) error {
auth := smtp.PlainAuth("", e.Username, e.Password, e.SMTPServer)
msg := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n"
msg += fmt.Sprintf("From: %s\r\n", e.From)
msg += fmt.Sprintf("To: %s\r\n", strings.Join(to, ";"))
msg += fmt.Sprintf("Subject: %s\r\n", subject)
msg += fmt.Sprintf("\r\n%s\r\n", content)
return smtp.SendMail(e.SMTPServer+":"+e.SMTPPort, auth, e.From, to, []byte(msg))
}
// GenerateEmailContent will return email content as a string.
func GenerateEmailContent() (string, error) {
type templateData struct {
Logo string
Thumbnail string
ServerURL string
ServerName string
Description string
StreamDescription string
}
td := templateData{
Logo: data.GetServerURL() + "/logo",
Thumbnail: data.GetServerURL() + "/thumbnail.jpg",
ServerURL: data.GetServerURL(),
ServerName: data.GetServerName(),
Description: data.GetServerSummary(),
StreamDescription: data.GetStreamTitle(),
}
t, err := template.New("goLive").Parse(goLiveTemplate)
if err != nil {
return "", errors.Wrap(err, "failed to parse go live email template")
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, td); err != nil {
return "", errors.Wrap(err, "failed to execute go live email template")
}
content := tpl.String()
return content, nil
}
|