-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmailer.go
61 lines (52 loc) · 1.29 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
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"crypto/tls"
"crypto/x509"
"net/smtp"
"github.com/jordan-wright/email"
)
// Mailer is a interface that any mailer should implement.
type Mailer interface {
SendEmail(emailAddress string, subject string, body []byte) error
}
// InitSMTPMailer creates a new SMTP Mailer
func InitSMTPMailer(config EmailConfig) Mailer {
var tlsConfig *tls.Config
if config.Cert != "" {
pool := x509.NewCertPool()
pool.AppendCertsFromPEM([]byte(config.Cert))
tlsConfig = &tls.Config{
ServerName: config.Host,
RootCAs: pool,
}
}
return &smtpMailer{
smtpHost: config.Host,
smtpPort: config.Port,
smtpUser: config.User,
smtpPass: config.Password,
smtpFrom: config.From,
tlsConfig: tlsConfig,
}
}
type smtpMailer struct {
smtpHost string
smtpPort string
smtpUser string
smtpPass string
smtpFrom string
tlsConfig *tls.Config
}
func (s *smtpMailer) SendEmail(emailAddress, subject string, body []byte) error {
e := email.NewEmail()
e.From = "cloud.gov <" + s.smtpFrom + ">"
e.To = []string{" <" + emailAddress + ">"}
e.Text = body
e.Subject = subject
addr := s.smtpHost + ":" + s.smtpPort
auth := smtp.PlainAuth("", s.smtpUser, s.smtpPass, s.smtpHost)
if s.tlsConfig != nil {
return e.SendWithTLS(addr, auth, s.tlsConfig)
}
return e.Send(addr, auth)
}