/
client.go
57 lines (49 loc) · 1.05 KB
/
client.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
// Package postfix is for sending mail
//
// import (
// "github.com/vanng822/postfix"
// )
//
// fromAddr := postfix.NewAddress(from, fromEmail)
// toAddr := postfix.NewAddress(to, toEmail)
// msg, err := postfix.MultipartMessage(fromAddr, toAddr, subject, text, html)
// if err != nil {
// log.Fatal("Postfix mailing with error", err)
// }
// err := postfix.Send(msg)
// if err != nil {
// log.Fatal("Postfix mailing with error", err)
// }
package postfix
import (
"fmt"
"net/smtp"
)
var (
host = "127.0.0.1"
port = 25
clientFactory func() (c *smtp.Client, err error)
)
func init() {
clientFactory = func() (c *smtp.Client, err error) {
c, err = smtp.Dial(getAddr())
return
}
}
// SetClientFactory for setting a smtp client if needed
func SetClientFactory(f func() (c *smtp.Client, err error)) {
clientFactory = f
}
func SetPort(p int) {
port = p
}
func SetHost(h string) {
host = h
}
func getAddr() string {
return fmt.Sprintf("%s:%d", host, port)
}
func getClient() (c *smtp.Client, err error) {
c, err = clientFactory()
return
}