/
mailer.go
158 lines (150 loc) · 4.05 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package mailer
import (
"bytes"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/mail"
"net/smtp"
"path"
"strings"
)
type Config struct {
MailHost string
MailPort string
MailUser string
MailPass string
}
type API struct {
Buffer *bytes.Buffer
Boundary interface{}
Config Config
Body struct {
To mail.Address
From mail.Address
Subject interface{}
Message interface{}
}
}
func (api *API) SetHeaders(from, to mail.Address, subject, message interface{}) {
api.Buffer = bytes.NewBuffer(nil)
api.SetBoundary()
api.Body.From = from
api.Body.To = to
api.Body.Subject = subject
api.Body.Message = message
header := []string{}
header = append(header, fmt.Sprintf("%s: %s", "To", api.Body.To.String()))
header = append(header, fmt.Sprintf("%s: %s", "From", api.Body.From.String()))
header = append(header, fmt.Sprintf("%s: %s", "Subject", api.Body.Subject))
header = append(header, fmt.Sprintf("%s: %s", "Mime-Version", "1.0"))
header = append(header, fmt.Sprintf("%s: %s", "Content-Type", `multipart/mixed;boundary="`+api.Boundary.(string)+`"`))
header = append(header, fmt.Sprintf("%s", ""))
header = append(header, fmt.Sprintf("--%s", api.Boundary.(string)))
header = append(header, fmt.Sprintf("%s", ""))
api.Buffer.WriteString(strings.Join(header, "\r\n"))
content := []string{}
content = append(content, fmt.Sprintf("%s: %s", "Content-Type", `text/html;charset=UTF-8`))
content = append(content, fmt.Sprintf("%s", ""))
content = append(content, fmt.Sprintf("%s", api.Body.Message))
api.Buffer.WriteString(strings.Join(content, "\r\n"))
return
}
func (api *API) SetBoundary() {
var buf [30]byte
io.ReadFull(rand.Reader, buf[:])
api.Boundary = fmt.Sprintf("%x", buf[:])
return
}
func (api *API) AttachFile(filepath string) bool {
if api.Boundary != nil {
file, err := ioutil.ReadFile(filepath)
if err == nil {
return api.AttachFileBytes(path.Base(filepath), file)
} else {
return false
}
} else {
return false
}
}
func (api *API) AttachFileBytes(name string, file []byte) bool {
if api.Boundary != nil {
attachment := []string{}
attachment = append(attachment, fmt.Sprintf("%s", ""))
attachment = append(attachment, fmt.Sprintf("--%s", api.Boundary.(string)))
attachment = append(attachment, fmt.Sprintf("%s: %s", "Content-Transfer-Encoding", `base64`))
attachment = append(attachment, fmt.Sprintf("%s: %s", "Content-Disposition", `attachment`))
attachment = append(attachment, fmt.Sprintf("%s: %s", "Content-Type", http.DetectContentType(file)+`;name="`+name+`"`))
attachment = append(attachment, fmt.Sprintf("%s", ""))
api.Buffer.WriteString(strings.Join(attachment, "\r\n"))
b := make([]byte, base64.StdEncoding.EncodedLen(len(file)))
base64.StdEncoding.Encode(b, file)
api.Buffer.WriteString("\r\n")
for i, l := 0, len(b); i < l; i++ {
api.Buffer.WriteByte(b[i])
if (i+1)%76 == 0 {
api.Buffer.WriteString("\r\n")
}
}
return true
} else {
return false
}
}
func (api *API) Send() bool {
if api.Boundary != nil {
api.Buffer.WriteString("\r\n")
api.Buffer.WriteString("--" + api.Boundary.(string) + "--")
addr := api.Config.MailHost + ":" + api.Config.MailPort
host, _, _ := net.SplitHostPort(addr)
auth := smtp.PlainAuth("", api.Config.MailUser, api.Config.MailPass, host)
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}
c, err := smtp.Dial(addr)
if err != nil {
log.Println(err)
return false
}
c.StartTLS(tlsconfig)
if err = c.Auth(auth); err != nil {
log.Println(err)
return false
}
if err = c.Mail(api.Body.From.Address); err != nil {
log.Println(err)
return false
}
if err = c.Rcpt(api.Body.To.Address); err != nil {
log.Println(err)
return false
}
w, err := c.Data()
if err != nil {
log.Println(err)
return false
}
_, err = w.Write(api.Buffer.Bytes())
if err != nil {
log.Println(err)
return false
}
err = w.Close()
if err != nil {
log.Println(err)
return false
}
c.Quit()
return true
} else {
return false
}
}