/
smtp.go
133 lines (123 loc) · 2.76 KB
/
smtp.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
/*
* Copyright 2015 Xuyuan Pang
* Author: Xuyuan Pang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package logo
import (
"crypto/tls"
"errors"
"io"
"net"
"net/smtp"
"strings"
"sync"
)
// SMTPWriter struct
type SMTPWriter struct {
cli *smtp.Client
subject string
addr string
username string
password string
to []string
buf []byte
mu sync.Mutex
}
// NewSMTPWriter create a new SMTPWriter instance.
func NewSMTPWriter(addr, username, password, subject string, to ...string) (*SMTPWriter, error) {
sw := &SMTPWriter{
addr: addr,
username: username,
password: password,
subject: subject,
to: to,
}
return sw, sw.connect()
}
// connect connects to SMTP server, init the client and init buffer.
func (sw *SMTPWriter) connect() error {
// init client
conn, err := tls.Dial("tcp", sw.addr, nil)
if err != nil {
return err
}
host, _, err := net.SplitHostPort(sw.addr)
if err != nil {
return err
}
c, err := smtp.NewClient(conn, host)
if err != nil {
return err
}
auth := smtp.PlainAuth("", sw.username, sw.password, host)
if err := c.Auth(auth); err != nil {
return err
}
if err := c.Mail(sw.username); err != nil {
return err
}
sw.cli = c
// init SMTP header
header := map[string]string{
"From": sw.username,
"To": strings.Join(sw.to, ";"),
"Subject": sw.subject,
"MIME-Version": "1.0",
"Content-Type": `text/plain; charset="utf-8"`,
}
var buf []byte
for k, v := range header {
buf = append(buf, k...)
buf = append(buf, ':', ' ')
buf = append(buf, v...)
buf = append(buf, '\r', '\n')
}
buf = append(buf, "\r\n\r\n"...)
sw.buf = buf
return nil
}
func (sw *SMTPWriter) Write(d []byte) (n int, err error) {
sw.mu.Lock()
if sw.cli == nil {
sw.mu.Unlock()
return -1, errors.New("client not init")
}
for _, t := range sw.to {
if err = sw.cli.Rcpt(t); err != nil {
sw.mu.Unlock()
return
}
}
var wc io.WriteCloser
if wc, err = sw.cli.Data(); err == nil {
body := append(sw.buf[:], d...)
n, err = wc.Write(body)
wc.Close()
sw.mu.Unlock()
return
}
sw.mu.Unlock()
return
}
// Close quites SMTP client.
func (sw *SMTPWriter) Close() (err error) {
sw.mu.Lock()
if sw.cli != nil {
err = sw.cli.Quit()
sw.cli = nil
}
sw.mu.Unlock()
return
}