-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.go
49 lines (42 loc) · 753 Bytes
/
mail.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
package main
import (
"fmt"
"net/smtp"
)
type SmtpClient struct {
Host string
Port string
User string
Password string
}
type Mail struct {
From string
To string
Subject string
Body string
}
func (c *SmtpClient) Send(m *Mail) error {
auth := smtp.PlainAuth(
"",
c.User,
c.Password,
c.Host,
)
header := make(map[string]string)
header["From"] = m.From
header["To"] = m.To
header["Subject"] = m.Subject
header["Content-Type"] = "text/plain; charset=\"utf-8\""
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + m.Body
return smtp.SendMail(
fmt.Sprintf("%s:%s", c.Host, c.Port),
auth,
m.From,
[]string{m.To},
[]byte(message),
)
}