/
send.go
106 lines (81 loc) · 1.92 KB
/
send.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
// Copyright 2014 The sutil Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package semail
import (
"net/mail"
"encoding/base64"
"net/smtp"
"fmt"
"strings"
)
type Email struct {
host string
from mail.Address
passwd string
}
func NewEmail(host, from, passwd, fromName string) *Email {
e := &Email {
host: host,
from: mail.Address{fromName, from},
passwd: passwd,
}
return e
}
func (m *Email) auth() smtp.Auth {
return smtp.PlainAuth(
"",
m.from.Address,
m.passwd,
m.host,
)
}
func (m *Email) encodeRFC2047(s string) string {
// use mail's rfc2047 to encode any string
addr := mail.Address{s, ""}
return strings.Trim(addr.String(), " <>")
}
func (m *Email) rmdup(addrs []string) []string {
ck := make(map[string]bool)
for _, a := range addrs {
if len(a) > 0 {
ck[a] = true
}
}
rv := make([]string, 0)
for k, _ := range ck {
rv = append(rv, k)
}
return rv
}
func (m *Email) Html(to, cc []string, subject, body string) error {
to = m.rmdup(to)
cc = m.rmdup(cc)
header := make(map[string]string)
header["From"] = m.from.String()
header["To"] = strings.Join(to, ";")
header["Cc"] = strings.Join(cc, ";")
header["Subject"] = m.encodeRFC2047(subject)
header["MIME-Version"] = "1.0"
//header["Content-Type"] = "text/plain; charset=\"utf-8\""
header["Content-Type"] = "text/html; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one ste
tolist := append(to, cc...)
tolist = m.rmdup(tolist)
//fmt.Println(tolist)
err := smtp.SendMail(
m.host + ":25",
m.auth(),
m.from.Address,
tolist,
[]byte(message),
)
return err
}