-
Notifications
You must be signed in to change notification settings - Fork 18
/
mailer.go
108 lines (96 loc) · 3.04 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
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
package W
import (
"crypto/tls"
"fmt"
"github.com/jordan-wright/email"
"github.com/kokizzu/gotro/A"
"github.com/kokizzu/gotro/I"
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/S"
"net/smtp"
)
type SmtpConfig struct {
Name string
Username string
Password string
Hostname string
Port int
}
func (mc *SmtpConfig) Address() string {
return mc.Hostname + `:` + I.ToStr(mc.Port)
}
func (mc *SmtpConfig) Auth() smtp.Auth {
return smtp.PlainAuth(``, mc.Username, mc.Password, mc.Hostname)
}
func (mc *SmtpConfig) From() string {
return mc.Name + ` <` + mc.Username + `>`
}
// run sendbcc on another goroutine
func (mc *SmtpConfig) SendBCC(bcc []string, subject string, message string) {
L.Print(`SendBCC started ` + A.StrJoin(bcc, `, `) + `; subject: ` + subject)
go mc.SendSyncBCC(bcc, subject, message)
}
// run sendAttachbcc on another goroutine
func (mc *SmtpConfig) SendAttachBCC(bcc []string, subject string, message string, files []string) {
L.Print(`SendAttachBCC started ` + A.StrJoin(bcc, `, `) + `; subject: ` + subject)
go mc.SendSyncAttachBCC(bcc, subject, message, files)
}
// sendbcc synchronous version, returns error message
func (mc *SmtpConfig) SendSyncBCC(bcc []string, subject string, message string) string {
return mc.SendSyncAttachBCC(bcc, subject, message, []string{})
}
// sendbcc synchronous version, returns error message
func (mc *SmtpConfig) SendSyncAttachBCC(bcc []string, subject string, message string, files []string) string {
e := email.NewEmail()
e.From = mc.From()
e.To = []string{e.From}
e.Bcc = bcc
e.Subject = subject
attach := A.StrJoin(files, ` `)
for _, file := range files {
_, err := e.AttachFile(file)
L.IsError(err, `SmtpConfig.SendSyncAttachBCC.AttachFile`)
}
if attach != `` {
attach = `; attachments: ` + attach
}
e.HTML = []byte(message + `<br/>
<br/>
--<br/>
Sincerely,<br/>
Automated Software<br/>
` + e.From)
L.Describe(e.Subject, e.Bcc)
err := e.Send(mc.Address(), mc.Auth())
if L.IsError(err, `failed to SendBCC`) {
return err.Error()
}
L.Print(`SendAttachBCC completed ` + A.StrJoin(bcc, `, `) + attach + `; subject: ` + subject)
return ``
}
func (m *SmtpConfig) SendRaw(e *email.Email) error {
if e.From == `` {
e.From = m.Username
}
//e := email.NewEmail()
//e.From = "Jordan Wright <[email protected]>"
//e.To = []string{"[email protected]"}
//e.Bcc = []string{"[email protected]"}
//e.Cc = []string{"[email protected]"}
//e.Subject = "Awesome Subject"
//e.Text = []byte("Text Body is, of course, supported!")
//e.HTML = []byte("<h1>Fancy HTML is supported, too!</h1>")
if S.EndsWith(m.Hostname, `gmail.com`) {
// gmail must use PlainAuth
return e.SendWithStartTLS(fmt.Sprintf("%s:%d", m.Hostname, m.Port), smtp.PlainAuth("", m.Username, m.Password, m.Hostname), &tls.Config{InsecureSkipVerify: true})
}
return e.Send(fmt.Sprintf("%s:%d", m.Hostname, m.Port), smtp.CRAMMD5Auth(m.Username, m.Password))
}
func NewMailer(user, pass, host string, port int) *SmtpConfig {
return &SmtpConfig{
Username: user,
Password: pass,
Hostname: host,
Port: port,
}
}