@@ -15,7 +15,7 @@ import (
15
15
"github.com/chenjie199234/Corelib/util/ctime"
16
16
)
17
17
18
- type EmailClientConfig struct {
18
+ type Config struct {
19
19
EmailName string `json:"email_name"`
20
20
MaxOpen uint16 `json:"max_open"` //0: default 100
21
21
MaxConnIdletime ctime.Duration `json:"max_conn_idletime"` //<=0 means no idle time out
@@ -24,12 +24,12 @@ type EmailClientConfig struct {
24
24
Account string `json:"account"`
25
25
Password string `json:"password"`
26
26
}
27
- type EmailClient struct {
28
- c * EmailClientConfig
27
+ type Client struct {
28
+ c * Config
29
29
p * cpool.CPool [* smtp.Client ]
30
30
}
31
31
32
- func NewEmailClient (c * EmailClientConfig ) (* EmailClient , error ) {
32
+ func NewEmail (c * Config ) (* Client , error ) {
33
33
if c .MaxOpen == 0 {
34
34
c .MaxOpen = 100
35
35
}
@@ -39,7 +39,7 @@ func NewEmailClient(c *EmailClientConfig) (*EmailClient, error) {
39
39
if c .Account == "" || c .Password == "" {
40
40
return nil , errors .New ("missing account/password in the config" )
41
41
}
42
- client := & EmailClient {
42
+ return & Client {
43
43
c : c ,
44
44
p : cpool .NewCPool (uint32 (c .MaxOpen ), func () (* smtp.Client , error ) {
45
45
client , e := smtp .Dial (c .Host + ":" + strconv .FormatUint (uint64 (c .Port ), 10 ))
@@ -60,16 +60,15 @@ func NewEmailClient(c *EmailClientConfig) (*EmailClient, error) {
60
60
}, c .MaxConnIdletime .StdDuration (), func (client * smtp.Client ) {
61
61
client .Close ()
62
62
}),
63
- }
64
- return client , nil
63
+ }, nil
65
64
}
66
- func (c * EmailClient ) SendTextEmail (ctx context.Context , to []string , subject string , body []byte ) error {
65
+ func (c * Client ) SendTextEmail (ctx context.Context , to []string , subject string , body []byte ) error {
67
66
return c .do (ctx , to , subject , "text/plain; charset=UTF-8" , body )
68
67
}
69
- func (c * EmailClient ) SendHtmlEmail (ctx context.Context , to []string , subject string , body []byte ) error {
68
+ func (c * Client ) SendHtmlEmail (ctx context.Context , to []string , subject string , body []byte ) error {
70
69
return c .do (ctx , to , subject , "text/html; charset=UTF-8" , body )
71
70
}
72
- func (c * EmailClient ) do (ctx context.Context , to []string , subject string , mimetype string , body []byte ) (e error ) {
71
+ func (c * Client ) do (ctx context.Context , to []string , subject string , mimetype string , body []byte ) (e error ) {
73
72
ctx , span := trace .NewSpan (ctx , "Corelib.Email" , trace .Client , nil )
74
73
span .GetSelfSpanData ().SetStateKV ("email" , c .c .EmailName )
75
74
span .GetSelfSpanData ().SetStateKV ("host" , c .c .Host + ":" + strconv .FormatUint (uint64 (c .c .Port ), 10 ))
@@ -115,7 +114,7 @@ func (c *EmailClient) do(ctx context.Context, to []string, subject string, mimet
115
114
return
116
115
}
117
116
}
118
- func (c * EmailClient ) formemail (to []string , subject string , mimetype string , body []byte ) []byte {
117
+ func (c * Client ) formemail (to []string , subject string , mimetype string , body []byte ) []byte {
119
118
//from
120
119
count := 6 + len (c .c .Account ) + 2
121
120
//to
@@ -154,7 +153,7 @@ func (c *EmailClient) formemail(to []string, subject string, mimetype string, bo
154
153
buf = append (buf , body ... )
155
154
return buf
156
155
}
157
- func (c * EmailClient ) sendemail (client * smtp.Client , to []string , email []byte ) (e error , del bool ) {
156
+ func (c * Client ) sendemail (client * smtp.Client , to []string , email []byte ) (e error , del bool ) {
158
157
defer func () {
159
158
if e != nil && client .Reset () != nil {
160
159
del = true
0 commit comments