-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.go
48 lines (41 loc) · 1.05 KB
/
mailer.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
package wildlifenl
import (
"log"
"strings"
"github.com/go-mail/mail"
)
type Mailer struct {
config *Configuration
}
func newMailer(config *Configuration) *Mailer {
return &Mailer{config: config}
}
func (e *Mailer) Ping() error {
if e.config.EmailHost == "no-email" {
return nil
}
s, err := e.dailer().Dial()
if err != nil {
return err
}
return s.Close()
}
func (e *Mailer) SendCode(appName, displayName, email, code string) error {
if e.config.EmailHost == "no-email" {
log.Println("Code for", email, "is:", code)
return nil
}
body := emailBody
body = strings.ReplaceAll(body, "{appName}", appName)
body = strings.ReplaceAll(body, "{displayName}", displayName)
body = strings.ReplaceAll(body, "{code}", code)
m := mail.NewMessage()
m.SetHeader("From", e.config.EmailFrom)
m.SetHeader("To", email)
m.SetHeader("Subject", emailSubject)
m.SetBody("text/html", body)
return e.dailer().DialAndSend(m)
}
func (e *Mailer) dailer() *mail.Dialer {
return mail.NewDialer(e.config.EmailHost, 587, e.config.EmailUser, e.config.EmailPassword)
}