-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
69 lines (59 loc) · 1.36 KB
/
mail.go
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
package passwordless
import (
"bytes"
"fmt"
log "github.com/Sirupsen/logrus"
"net/smtp"
"os"
"strings"
"text/template"
)
var (
auth smtp.Auth
authParts map[string]string
fromEmail string
)
var emailTemplate = template.Must(template.New("emailTemplate").Parse(`From: {{.From}}
To: {{.To}}
Subject: {{.Subject}}
{{.Body}}`))
func SendMail(to []string, subject, message string) error {
var doc bytes.Buffer
ctx := struct {
From string
To string
Subject string
Body string
}{
fromEmail,
strings.Join(to, ", "),
subject,
message,
}
if err := emailTemplate.Execute(&doc, ctx); err != nil {
return err
}
return smtp.SendMail(
fmt.Sprintf("%v:%v", authParts["EMAIL_HOST"], authParts["EMAIL_PORT"]),
auth,
fromEmail,
to,
doc.Bytes())
}
func init() {
authParts = make(map[string]string)
// get from address
fromEmail = os.Getenv("EMAIL_FROM")
if fromEmail == "" {
log.Panic("Missing required environment variable 'EMAIL_FROM'.")
}
// initialize smtp auth
for _, part := range []string{"EMAIL_HOST_USER", "EMAIL_HOST_PASSWORD", "EMAIL_HOST", "EMAIL_PORT"} {
envPart := os.Getenv(part)
if envPart == "" {
log.Panicf("Missing required environment variable '%s'.", part)
}
authParts[part] = envPart
}
auth = smtp.PlainAuth("", authParts["EMAIL_HOST_USER"], authParts["EMAIL_HOST_PASSWORD"], authParts["EMAIL_HOST"])
}