-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.go
More file actions
99 lines (79 loc) · 2.25 KB
/
mail.go
File metadata and controls
99 lines (79 loc) · 2.25 KB
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
package mail
import (
"crypto/tls"
"errors"
"github.com/sec51/goconf"
gomail "gopkg.in/gomail.v2"
)
type EmailConfig struct {
// The hostname or ip address of the mail server
Server string
// The mail server submission port
Port int
// the From email: can be in the form: "Info <info@sec51.com>"
From string
// The username to authenticate
Username string
// The password to authenticate
Password string
// whether to make a TLS connection
UseTLS bool
// whether to use username and password for authentication
UseAUTH bool
// Skip verification of TLS certificate
InsecureSkipVerify bool
}
func (c *EmailConfig) Send(body, to, subject string) error {
var d *gomail.Dialer
m := gomail.NewMessage()
m.SetHeader("To", to)
m.SetHeader("Subject", subject)
m.SetBody("text/html", body)
from := c.From
if from == "" {
from = goconf.AppConf.String("smtp::from")
if from == "" {
return errors.New("From email address not configured - set: smtp::from = FROM in the config file")
}
}
m.SetHeader("From", from)
smtpServer := c.Server
if smtpServer == "" {
smtpServer = goconf.AppConf.String("smtp::server")
if smtpServer == "" {
return errors.New("SMTP server not configured - set: smtp::server = HOSTNAME in the config file")
}
}
smtpUser := c.Username
if smtpUser == "" {
smtpUser = goconf.AppConf.DefaultString("smtp::username", c.Username)
}
smtpPass := c.Password
if smtpPass == "" {
smtpPass = goconf.AppConf.DefaultString("smtp::password", c.Password)
}
smtpPort := c.Port
if smtpPort == 0 {
smtpPort = goconf.AppConf.DefaultInt("smtp::port", c.Port)
if smtpPort == 0 {
return errors.New("SMTP port not configured, set: smtp::port = PORT in the config file")
}
}
if c.UseAUTH {
if smtpUser == "" {
return errors.New("SMTP username not configured, set: smtp::username = NAME in the config file")
}
if smtpPass == "" {
return errors.New("SMTP password not configured, set: smtp::password = PASSWORD in the config file")
}
d = gomail.NewDialer(smtpServer, smtpPort, smtpUser, smtpPass)
} else {
d = &gomail.Dialer{Host: smtpServer, Port: smtpPort}
}
d.SSL = c.UseTLS
d.TLSConfig = &tls.Config{
InsecureSkipVerify: c.InsecureSkipVerify,
ServerName: smtpServer,
}
return d.DialAndSend(m)
}