-
Notifications
You must be signed in to change notification settings - Fork 2
/
email.go
118 lines (107 loc) · 3.09 KB
/
email.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package pezauth
import (
"bytes"
"fmt"
"log"
"net/smtp"
"os"
"strconv"
"text/template"
"github.com/cloudfoundry-community/go-cfenv"
)
//NewEmailServerFromService - construct email server from vCap Service
func NewEmailServerFromService(appEnv *cfenv.App) *EmailServer {
serviceName := os.Getenv("SMTP_SERVICE_NAME")
hostName := os.Getenv("SMTP_HOST")
portName := os.Getenv("SMTP_PORT")
userName := ""
passName := ""
supportEmail := os.Getenv("SUPPORT_EMAIL")
service, err := appEnv.Services.WithName(serviceName)
if err != nil {
panic(fmt.Sprintf("email service name error: %s", err.Error()))
}
if service.Credentials[hostName] == nil {
panic(fmt.Sprintf("credentials contains a nil value for (%s, %s or %s): %v", userName, passName, hostName, service.Credentials))
}
auth := smtp.PlainAuth("", userName, passName, service.Credentials[hostName].(string))
port, err := strconv.Atoi(service.Credentials[portName].(string))
if err != nil {
panic(fmt.Sprintf("The port for email server is not a valid integer %s", err.Error()))
}
return &EmailServer{
host: service.Credentials[hostName].(string),
port: port,
auth: auth,
sendMailFunc: DefaultSMTPSendEmail,
supportEmail: service.Credentials[supportEmail].(string),
}
}
//DefaultSMTPSendEmail - This is the default SMTP server send email behavior
//There are some issue with the smtp ssl certificate
//Reimplementing the http://golang.org/src/net/smtp/smtp.go?s=7610:7688#L263
//Will switch back to the default smtp.SendMail function
func DefaultSMTPSendEmail(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(msg)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
//SendMailFunc - Function to wrap the smtp SendMail behavior
type SendMailFunc func(string, smtp.Auth, string, []string, []byte) error
//NewEmailServer - Create an email server
func NewEmailServer(host string, port int, auth smtp.Auth, sendMailFunc SendMailFunc) *EmailServer {
return &EmailServer{
host: host,
port: port,
auth: auth,
sendMailFunc: sendMailFunc,
}
}
//SendEmail - send email
func (emailServer *EmailServer) SendEmail(data *SMTPData) error {
var doc bytes.Buffer
t := template.New("emailTemplate")
t, err := t.Parse(SMTPTemplate)
if err != nil {
log.Fatal("error trying to parse mail template", err)
return err
}
err = t.Execute(&doc, data)
if err != nil {
log.Fatal("error tring to map data to the smtp email template", err)
return err
}
err = emailServer.sendMailFunc(emailServer.host+":"+strconv.Itoa(emailServer.port),
emailServer.auth,
data.From,
[]string{data.To},
doc.Bytes())
return err
}
//GetSupportEmail - retrieve the support email address
func (emailServer *EmailServer) GetSupportEmail() string {
return emailServer.supportEmail
}